필터 지우기
필터 지우기

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

조회 수: 5 (최근 30일)
Karl
Karl 2011년 7월 13일
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