Borrar filtros
Borrar filtros

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

5 visualizaciones (últimos 30 días)
Karl
Karl el 13 de Jul. de 2011
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!

Respuesta aceptada

the cyclist
the cyclist el 13 de Jul. de 2011
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 comentarios
Karl
Karl el 13 de Jul. de 2011
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 el 13 de Jul. de 2011
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.

Iniciar sesión para comentar.

Más respuestas (3)

Walter Roberson
Walter Roberson el 13 de Jul. de 2011
spectra{j} = zeros(1340,4);
  5 comentarios
Karl
Karl el 13 de Jul. de 2011
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 el 13 de Jul. de 2011
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.

Iniciar sesión para comentar.


the cyclist
the cyclist el 14 de Jul. de 2011
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 el 13 de Jul. de 2011
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.

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by