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);

采纳的回答

Jan
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:
  1. "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
  2. Or Res is empty because the imported file is empty.

更多回答(1 个)

James Tursa
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 个评论
Randy st
Randy st 2017-5-10
I'm pretty sure I know why. It's because the pre allocation Rmax=zeroes (L,1) assigns a Lx1 Matrix to RMax, then I try to store a single element vector in Rmax (i) below...however, I don't know how to fix it.
Stephen23
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 CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by