min(A,[],'all') returning 0 in script but number in command window ...

high_y in the following code returns 0, but when the line is run in the command window i get the correct answer, below is my code:
% CSI viewer for deuterium spectroscopy
% Load in data
clear all; close all; clc
% Load in background image
Path = 'C:\Users\ppydjco\Pictures\CSI';
SpecFile='background_2.png';
cd (Path)
bg = imread(SpecFile);
figure(1)
imagesc(bg)
set(gca, 'xtick', [], 'ytick', [], 'xticklabel',[],'yticklabel',[]);
%% Load CSI data
Path = 'C:\Users\ppydjco\Documents\PhD\Data_March_2021\7T_phantom';
% SpecFile = 'deuterium_leg_5_WIP_2HRFtest_single_Experiment1_7_1_raw_act';
% SpecFile='deuterium_leg_5_WIP_2HRFtest_single_Experiment1_11_1_raw_act';
SpecFile = '2htest_2H2DCSI_Experiment1_72_1_raw_act';
cd (Path)
fid = mrs_readSDAT(SpecFile);
info = mrs_readSPAR(SpecFile);
disp('Header information: ')
disp(info); %info.BW=info.BW/2;
[Nsamp,Nrow,Ncol] = size(fid);
xpts = (1:Nsamp).';
ppm = mrs_points2ppm(xpts, Nsamp, info.BW, info.transmit_frequency);
freq = mrs_points2Hz(xpts, Nsamp, info.BW);
% [ax1, ~] = tight_subplot(Nrow,Ncol,[.005 .002],[.05 .02],[.01 .01]);
% for i = 1: Nrow*Ncol
% axes(ax1(i))
% plot(real(fid(:,i)))
% ys=get(gca,'YLim');
% axis([0 info.samples ys]);
% set(gca,'XTick',[], 'YTick', []) % For data not on background
% col = get(gcf, 'color'); % Data on background
% set(gca, 'box', 'on', 'xtick', [], 'color', 'none', ...
% 'xticklabel',[],'yticklabel',[]);
% end
spect = mrs_fft(fid);
spect_pc = zeros(size(fid));
phi = zeros(Nrow, Ncol);
figure(1)
[ax2, ~] = tight_subplot(Nrow,Ncol,[.00 .00],[.11 .075],[.13 .095]);
high_y = max(real(spect_pc),[],'all'))
low_y = min(real(spect_pc));
for i = 1: Nrow*Ncol
axes(ax2(i))
[spect_pc(:,i), phi(i)] = mrs_zeroPHC(spect(:,i));
plot(ppm,real(spect_pc(:,i))); set(gca,'xdir','reverse');
% xlim([-20 20]); ylim([-500 1000]) % 3T Leg Data
% xlim([-20 0]); ylim([-500 21000]) % 3T Phantom data
xlim([-10 0]); ylim([-6 31]) % 7T phantom data
% set(gca,'XTick',[], 'YTick', []) % For data not on background
col = get(gcf, 'color'); % Data on background
set(gca, 'box', 'on', 'xtick', [], 'color', 'none', ...
'xticklabel',[],'yticklabel',[]);
end
figure
imagesc(phi)
colorbar
disp('*******************************************')
disp('******************Done*********************')
disp('*******************************************')
Also when I run which('max', '-all') I get the following:
>> which('max', '-all')
built-in (C:\Program Files\MATLAB\R2020b\toolbox\matlab\datafun\max)
built-in (C:\Program Files\MATLAB\R2020b\toolbox\matlab\datafun\@double\max) % double method
built-in (C:\Program Files\MATLAB\R2020b\toolbox\matlab\datafun\@int16\max) % int16 method
built-in (C:\Program Files\MATLAB\R2020b\toolbox\matlab\datafun\@int32\max) % int32 method
built-in (C:\Program Files\MATLAB\R2020b\toolbox\matlab\datafun\@int64\max) % int64 method
built-in (C:\Program Files\MATLAB\R2020b\toolbox\matlab\datafun\@int8\max) % int8 method
built-in (C:\Program Files\MATLAB\R2020b\toolbox\matlab\datafun\@logical\max) % logical method
built-in (C:\Program Files\MATLAB\R2020b\toolbox\matlab\datafun\@single\max) % single method
built-in (C:\Program Files\MATLAB\R2020b\toolbox\matlab\datafun\@uint16\max) % uint16 method
built-in (C:\Program Files\MATLAB\R2020b\toolbox\matlab\datafun\@uint32\max) % uint32 method
built-in (C:\Program Files\MATLAB\R2020b\toolbox\matlab\datafun\@uint64\max) % uint64 method
built-in (C:\Program Files\MATLAB\R2020b\toolbox\matlab\datafun\@uint8\max) % uint8 method
max is a built-in method % slreq.datamodel.AttributeKindEnum method
max is a built-in method % slreq.datamodel.AttributeRegType method
max is a built-in method % slreq.datamodel.AttributeTypeEnum method
max is a built-in method % slreq.datamodel.CustomIdNumbering method
max is a built-in method % slreq.datamodel.MappingDirectionEnum method
max is a built-in method % matlab.lang.OnOffSwitchState method
max is a Java method % java.util.Collections method
max is a built-in method % matlab.internal.timer.CallBackTypeEnum method
max is a built-in method % matlab.internal.timer.BusyModeEnum method
max is a built-in method % matlab.internal.timer.ExecutionModeEnum method
C:\Program Files\MATLAB\R2020b\toolbox\matlab\datatypes\categorical\@categorical\max.m % categorical method
C:\Program Files\MATLAB\R2020b\toolbox\matlab\datatypes\datetime\@datetime\max.m % datetime method
C:\Program Files\MATLAB\R2020b\toolbox\matlab\datatypes\duration\@duration\max.m % duration method
C:\Program Files\MATLAB\R2020b\toolbox\matlab\bigdata\@tall\max.m % tall method
C:\Program Files\MATLAB\R2020b\toolbox\matlab\timeseries\@timeseries\max.m % timeseries method
C:\Program Files\MATLAB\R2020b\toolbox\finance\ftseries\@fints\max.m % fints method
C:\Program Files\MATLAB\R2020b\toolbox\coder\half\@half\max.p % half method
C:\Program Files\MATLAB\R2020b\toolbox\mbc\mbcmodels\@localpoly\max.m % localpoly method
C:\Program Files\MATLAB\R2020b\toolbox\mbc\mbcmodels\@localpspline\max.m % localpspline method
C:\Program Files\MATLAB\R2020b\toolbox\nnet\deep\@dlarray\max.m % dlarray method
C:\Program Files\MATLAB\R2020b\toolbox\parallel\parallel\@codistributed\max.m % codistributed method
C:\Program Files\MATLAB\R2020b\toolbox\parallel\gpu\@gpuArray\max.m % gpuArray method
C:\Program Files\MATLAB\R2020b\toolbox\symbolic\symbolic\@sym\max.m % sym method
>>
Finally I'm on windows on Matlab 2020b

 Accepted Answer

spect_pc = zeros(size(fid));
. . .
high_y = max(real(spect_pc),[],'all'))
high_y is 0 because all values of spect_pc are 0s. That's the correct output.
Later in your code you change the values of spect_pc
[spect_pc(:,i), phi(i)] = mrs_zeroPHC(spect(:,i));
You might be running sections of the code without clearing the variables between runs (sloppy), which changes the the max value of spect_pc on subsequent runs.
The solution is to run the line high_y=max(...) before the values of spect_pc are changed.

2 Comments

Can't believe I didn't see that thank you for pointing it out, the original zeros part is to create an empty array to be filled in on the loop. I need to seperate calculation from the plot.
That's good that you're preallocating the loop variable. Just be aware of what operations have been done on the variable before you test it so you have an appropriate expectation of its value.
Also, separating computations and visualizations is usually the best way to go. Sounds like you're heading in the right direction.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!