why do I get "Matrix dimensions must agree" when using "<"

1 visualización (últimos 30 días)
Rong
Rong el 21 de Oct. de 2014
Comentada: Matt Tearle el 21 de Oct. de 2014
iless={};
igreater={};
TimeVecmin={};
T1=T-60;
T2=T+60;
for i = 1:length(MiceCond)
TimeVecmin{i}=TimeVec{i}*20/1000/60;
iless{i}=TimeVecmin{i}<T & TimeVecmin{i}>T1;
igreater{i}=TimeVecmin{i}>T & TimeVecmin{i}<T2;
end
in the above code, T is the number that the user give the input, eg. 60 or 70. TimeVec is a 1*6 cell array. I can run
iless{1}=TimeVecmin{1}<T & TimeVecmin{1}>T1;
or any given number for i, but once I put it within the cycle, it shows the Matrix dimension error. How does this happen?

Respuestas (3)

Orion
Orion el 21 de Oct. de 2014
Editada: Orion el 21 de Oct. de 2014
Hi,
What is the length of MiceCond ?
if length(MiceCond) is more than 6 (length of TimeVec) then you're gonna get an error.
  1 comentario
Rong
Rong el 21 de Oct. de 2014
MiceCond is a 1*6 cell array. So the length of MiceCond is 6 also.

Iniciar sesión para comentar.


Matt Tearle
Matt Tearle el 21 de Oct. de 2014
Editada: Matt Tearle el 21 de Oct. de 2014
Given the form of the error message and your comment that it works for a given value of i (presumably at the command line), but not in the loop (presumably running a script/function), I'm going to take a guess that somehow you're getting a different T than what you're using when you try it manually for a given i.
Can you show the code for where T comes from? Or just set a breakpoint and check what T is when the error occurs:
dbstop if error
Then run your program. When the error happens, you'll enter debug mode. Check the value of T. Then dbquit when you're done.
I'm speculating that T is a string or something like that. So it's being converted into a 2-element numeric vector, hence the error.
[Oh, and dbclear if error when you're done figuring out the problem and don't want it to stop on errors anymore :)]
  2 comentarios
Rong
Rong el 21 de Oct. de 2014
Editada: Image Analyst el 21 de Oct. de 2014
here it is:
[fileName, pathName] = uigetfile('*.txt', 'Select the .txt file', path);
data = importdata([pathName fileName]);
T=fileName;
stimstart=input(['When was stimulation started for ', T, ' (in min)? ']);
So T is given when file is loaded. the file is txt file.
Matt Tearle
Matt Tearle el 21 de Oct. de 2014
So T is a file name. Even if you try to turn that into a number somehow (which doesn't happen in the code you've shown), there are many ways for that to go horribly wrong. You should definitely examine what T is in the loop (or just before it). Either use the dbstop approach I mentioned or simply add disp(T) and/or whos T right before the loop. What do you get?
Are you sure it's not stimstart you want to be using instead of T...?

Iniciar sesión para comentar.


Orion
Orion el 21 de Oct. de 2014
so T is a string (= fileName)?
and then you do
T1=T-60;
T2=T+60;
it seems that you need to convert your data.
  1 comentario
Rong
Rong el 21 de Oct. de 2014
Thank you! But I add as T=str2num('T');
it still shows
Error using < Matrix dimensions must agree.
Error in feed20state2 (line 65) iless{i}=TimeVecmin{i}<T & TimeVecmin{i}>T1;

Iniciar sesión para comentar.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by