- "max" is not the function max() but has been shadowed by a variables with the same name. Use the debugger as explained by James and when Matlab stops type: which max -all
- Or Res is empty because the imported file is empty.
Anyone know what I am getting this error?
조회 수: 1 (최근 30일)
이전 댓글 표시
if true
% code
end
function [RMax, RMaxHalf]= Mathworks_HalfMaxtimesRational()
%Grabbing your folder,grabbing files of interest, assigning length of folder, formatting documents,
delimiter = ','; %The comma is used to separate matrix subscripts and arguments to functions.
startRow = 25; %specifying the first row you want data from
fmt=[repmat('%f',1,2) '%*[^\n]']; %The fmt variable is the format string that maps the content of the file to the data variables you want returned--above it is two string variables and then an idiom to skip the rest of each record (line).
pn = uigetdir(pwd,'matlabbatch'); % Getting directory/folder from memory
d=dir(fullfile(pn, '*.csv')); %getting specific files from directory/folder
L=length(d); % so now preallocate knowing length...
RMax=zeros(L,1);
RTime_Half=RMax;
% and loop over the files...
for i=1:L
fid=fopen(d(i).name,'r'); %opening file # i
dataArray=cell2mat(textscan(fid, fmt, 'Delimiter', delimiter, 'headerlines', startRow, 'collectoutput',1));
fid=fclose(fid); %closing the file
Time_Hrs = dataArray(:,1);
Res = dataArray(:,2);
RMax(i)= max(Res);
RMaxHalf(i)=RMax(i)/2;
tmp=abs(Res-RMaxHalf(i));
[~,idx] = min(tmp);
RTime_Half(i) = Time_Hrs(idx);
end
xlswrite('Time_Half_Max.xlsx', [RMax RTime_Half])
ERROR MESSAGE In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in Mathworks_HalfMaxtimesRationalathome (line 18) RMax(i)= max(Res);
댓글 수: 0
채택된 답변
Jan
2017년 5월 10일
편집: Jan
2017년 5월 10일
Rmax=zeroes(L,1) assigns a Lx1 Matrix to RMax, then I try to store a single
element vector in Rmax(i)
Correct. This is the right way to assign values to a pre-allocated vector. You have another problem: "RMax(i)= max(Res)" fails, if the number of elements on the left and on the right are different. On the left is a scalar, so on the right there must be something different. This can be cause by two reasons:
댓글 수: 0
추가 답변 (1개)
James Tursa
2017년 5월 10일
Use the debugger. First, type the following:
dbstop if error
Then run your code. When the error occurs, the program will pause with all variables intact. Examine the variables in question to see what the sizes are etc. Then backtrack in your code to figure out why the sizes are not as expected.
댓글 수: 2
Stephen23
2017년 5월 10일
@Randy st: "I'm pretty sure I know why"... guessing how code works, and guessing why it is not working, is not a productive technique. Use the debugging tools, as James Tursa wrote. Then you will know why it is not working.
참고 항목
카테고리
Help Center 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!