Info

This question is closed. Reopen it to edit or answer.

GUI error : Subscripted assignment dimension mismatch.

3 views (last 30 days)
n = [1 2 3 4];
for i= 1:4
h = n(i) ;
Ca1 = x(1:4);
Cb1 = x(5:8);
Ca2 = x(9:12);
Cb2 = x(13:16);
fun (i) = 1/tiempo*(Cao - Ca1(i)) - (k*Ca1(i)).^h;
fun (i+4) = -1/tiempo*Cb1(i) + (k*Ca1(i)).^h;
fun(i+8) = 1/tiempo*(Ca1(i) - Ca2(i)) - (k*Ca2(i)).^h;
fun (i+12) = 1/tiempo*(Cb1(i) - Cb2(i)) - (k*Ca2(i)).^h;
end
This are the functions I use in order to solve a problem. (This goes in a different script)
When I run my GUI the message: Subscripted assignment dimension mismatch. And the error appears to be the functions (fun) in my other script. Just to clarify, this is an EDO problem and in my code in the GUI I did this:
[t,x] = ode45 (@Proy, tint, xo); (example)
The name of my other scrip is "Proy".
  3 Comments
Daniels Frias
Daniels Frias on 12 Nov 2017
Edited: Daniels Frias on 12 Nov 2017
MY ERROR NOW IS THE FOLLOWING: SWITCH EXPRESSION MUST BE A SCALAR OR A CHARACTER VECTOR Okay. By EDO I meant ODE. Instead of writing my whole code in the GUI, I wrote my functions in a different script(called Proy) and then when I used ODE, I basically call the script Proy with an @. Here is my code again:(I changed it a little) Script Proy:
function fun = Proy(t,x)
global tiempo
global k
Ca1 = x(1);
Cb1 = x(2);
Ca2 = x(3);
Cb2 = x(4);
Cao = 20;
fun (1) = 1/tiempo*(Cao - Ca1) - k*Ca1;
fun (2) = -1/tiempo*Cb1 + k*Ca1;
fun(3) = 1/tiempo*(Ca1 - Ca2) - k*Ca2;
fun (4) = 1/tiempo*(Cb1 - Cb2)-k*Ca2;
fun = fun';
Script in GUI
global tiempo
global te
global R
global k
contenido = get(handles.opcion,'String');
q = get(handles.opcion,'Value');
preg= contenido(q);
switch preg
case 'Reacción 1: A-->B'
k = 0.0157;
case 'Reacción 2: C-->D'
k = 0.12;
case 'Reacción 3: E-->F'
k = 0.089;
case 'Reacción 4: G-->H'
k = 0.35;
end
tiempo = eval(get(handles.residencia,'String'));
xo = [0 0 0 0];
if tiempo <= 10
tint = [0,100];
[t,x] = ode45 (@Proy, tint, xo);
elseif tiempo <= 45
tint = [0,250];
[t,x] = ode45 (@Proy, tint, xo);
else
tint = [0,450];
[t,x] = ode45 (@Proy, tint, xo);
end
R = get(handles.decision,'String');
switch R
case 'Sí'
te = eval(get(handles.TE,'String'));
Ca1e = x(te,1);
Cb1e = x(te,2);
Ca2e = x(te,3);
Cb2e = x(te,4);
case 'No'
Ca1 = x(:,1) ;
Cb1 = x(:,2);
Ca2 = x(:,3);
Cb2 = x(:,4);
C = [Ca1 Cb1 Ca2 Cb2];
end
And a picture of my GUI:
%
Jan
Jan on 12 Nov 2017
Writing in UPPERCASE means shouting in the forum. Therefore let me ask you to calm down. Thanks.

Answers (1)

Jan
Jan on 12 Nov 2017
Edited: Jan on 12 Nov 2017
ERROR NOW IS THE FOLLOWING: SWITCH EXPRESSION MUST BE A SCALAR
OR A CHARACTER VECTOR
Again: Please post the complete error message, not just a part of it. It is really inefficient to let the readers guess the details.
Obviously "Proy" is not a script, but a function. This is an important difference.
tiempo = eval(get(handles.residencia,'String'));
eval is an ugly command. There are always better methods. I assume you want sscanf. Note what happens, if a funny user types this in your edit field:
'system(''format D:'')'
Then your eval will destroy the data on the disk D. What a bad idea.
Using global variables is a source of bugs and unexpected side effects. Provide parameters by anonymous functions: http://www.mathworks.com/matlabcentral/answers/1971
The cause of the error is:
preg = contenido(q);
Then preg is a scalar cell string, but as the error message tells you, you need a string:
preg = contenido{q}; % Curly braces
A simplification:
if tiempo <= 10
tint = [0,100];
elseif tiempo <= 45
tint = [0,250];
else
tint = [0,450];
end
[t,x] = ode45 (@Proy, tint, xo);
Avoid duplicate code in programs to reduce the number of lines, which have to be debugged or changed in case of modifications.
  3 Comments
Jan
Jan on 13 Nov 2017
@Daniels Frias: You need help and I am glad, when I can help you. Please do not treat me, like I am super intelligent or all-knowing. Even if you are new at programming GUIs you know much more about your code and the problem, then I do. I do not have the faintest idea and cannot know anything, which you do not provide. I (and all other readers of your question) need clear explanation about what your are doing and the complete error message. If I have to ask twice for it, I get the impression that you do not want to make it as easy as possible to help you. You still did not tell us, which line is causing the "Subscripted assignment dimension mismatch" error or if this problem is solved now.
As far as I can see, Walter has formatted your code in the question now. Thanks, Walter!
The frequent contributors in this forum inform newcomers multiple times each day to format the code properly. Asking for the error messages is daily routine also, as well as remembering what UPPERCASE means in all forums. We warn several times each day, when eval or globals are used, see e.g. https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. The differences between scripts and functions are explained repeatedly also.
Asking questions for clarifications and providing suggestions for improving the code is not meant personally. All I want to do is helping you to solve your problem, but this should not be made harder then necessary.
Is the problem with the code solved now?
Daniels Frias
Daniels Frias on 13 Nov 2017
Yes!! Thank you so much. Now I have other errors but I might create another question.

This question is closed.

Community Treasure Hunt

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

Start Hunting!