Unclear as to why I'm getting a dimension mismatch?

5 次查看(过去 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!

采纳的回答

the cyclist
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 个评论
Karl
Karl 2011-7-13
Hey Cyclist,
Thanks for the response. I'd love to see a suggested bit of code using that method. I spent a good amount of time figuring out the eval method only to come back and see you having already posted it! Man...
But if your willing, how would I do this using the cell arrays?
the cyclist
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
Walter Roberson 2011-7-13
spectra{j} = zeros(1340,4);
  5 个评论
Karl
Karl 2011-7-13
Not really sure how I'd use a cell array in this situation so I'm posting hoping this will get your attention and you'll be so kind as to give me a hint(aka some code). ;)
Walter Roberson
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
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

Daniel Shub
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.

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by