Unclear as to why I'm getting a dimension mismatch?
1 次查看(过去 30 天)
显示 更早的评论
This is the bit of code that is giving me trouble:
>> strcat('spectra',num2str(j)) = zeros(1340,4);
??? Subscripted assignment dimension mismatch.
Only thing I can think of is that matlab believes I'm attempting to assign a matrix into a string. Where as what I'm trying to do is assign the string as the variable for a matrix. For reference j is just an integer counter.
edit Basically trying to create an increasing variable creation so that for each loop of the for fxn I get a new matrix with the assigned variable that then has memory preallocated.
edit2
eval(['spectra' int2str(j) '=zeros(1340,4);'])
Is what I finally settled on... the full loop:
for j=1:size(filelist,1)
eval(['spectra' int2str(j) '=zeros(1340,4);'])
eval(['spectra' int2str(j) '(1:1340,3:4)=xlsread(filelist(j,1:end));'])
for k=1:1340
eval(['spectra' int2str(j) '(k,1)= k*correction(1,1)+correction(1,2);'])
eval(['spectra' int2str(j) '(k,2)=(10^7)/532-spectra' int2str(j) '(k,1);'])
eval(['spectra' int2str(j) '(k,3)=1/spectra' int2str(j) '(k,2)*(10^7);'])
end
end
I've seen some mentions of increased speed doing other methods. While this isn't really a time intensive script, I wouldn't mind speeding it up a bit so its a bit more responsive when I'm working.
Thanks for the responses all!
0 个评论
采纳的回答
the cyclist
2011-7-13
This is how to do what you are asking to do:
eval([['spectra',num2str(j)],' = zeros(1340,4)']);
However, Walter is absolutely correct that cell arrays are the way to go on this. Use his syntax, and you will have variables effectively named "spectra{1}", "spectra{2}", etc instead of spectra1, spectra2, etc. Your code will be more readable and efficient, too.
2 个评论
the cyclist
2011-7-13
Heading out the door so can't write it up, but basically wherever you have "spectra int2str(j)" just use spectra{j} instead and you don't need the eval. Note the curly brackets instead of parentheses.
更多回答(3 个)
Walter Roberson
2011-7-13
spectra{j} = zeros(1340,4);
5 个评论
Walter Roberson
2011-7-13
for j=1:size(filelist,1)
spectra{j}=zeros(1340,4);
spectra{j}(1:1340,3:4)=xlsread(filelist(j,1:end));
and so on.
the cyclist
2011-7-14
I believe this is the correct transliteration of your code into cell array syntax. I have to admit I have not tried executing it, though. Too lazy to figure out the Excel file, etc.
lengthFileList = size(filelist,1);
spectra = cell(lengthFileList,1);
for j=1:lengthFileList
spectra{j}=zeros(1340,4);
spectra{j}(1:1340,3:4)=xlsread(filelist(j,1:end));
for k=1:1340
spectra{j}(k,1)= k*correction(1,1)+correction(1,2);
spectra{j}(k,2)=(10^7)/532-spectra1(k,1);
spectra{j}(k,3)=1/spectra1(k,2)*(10^7);
end
end
0 个评论
Daniel Shub
2011-7-13
First, Walter is correct that you likely don't want to do what you are trying to do. Second, I don't think you are doing what you think you are doing. Third, I am a little surprised by the error you get. I believe what you are really doing is equivalent to:
clear x;
x(double('spectra'),double(num2str(1))) = zeros(1340, 4);
strcat = x;
since
double('spectra')
equals
ans =
115 112 101 99 116 114 97
there is a dimension mismatch.
0 个评论
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!