Using assignin command in guide

조회 수: 6 (최근 30일)
Luffy
Luffy 2012년 7월 5일
%The below line is in base workspace
C = char('red','red','red');
%The below code is in gui created by guide.
a = get(handles.popupmenu1,'Value') % Based on value from popup menu1 i have to decide where i should set option selected in popup menu2
val = get(handles.popupmenu2,'Value');
switch val
case 1
assignin('base','C(a,:)','red');
case 2
assignin('base','C(a,:)','blue');
case 3
assignin('base','C(a,:)','black');
end
It shows an error Invalid variable name "C(a,:)" in ASSIGNIN.

채택된 답변

TAB
TAB 2012년 7월 5일
편집: TAB 2012년 7월 5일
switch val
case 1
evalin('base',['C(' num2str(a) ',:)=''abc''']);
case 2
evalin('base',['C(' num2str(a) ',:)=''lmn''']);
case 3
evalin('base',['C(' num2str(a) ',:)=''xyz''']);
end
As your matrix C is predefined as 3x3, you can not assign C(a,:)='blue' because 'blue' has 4 columns while all rows in your matrix contain 3 columns.
Use cell array for such operations.
Using cell array, in base workspace
C={'red','red','red'};
In GUI function
switch val
case 1
evalin('base',['C{' num2str(a) '}=''red''']);
case 2
evalin('base',['C{' num2str(a) '}=''blue''']);
case 3
evalin('base',['C{' num2str(a) '}=''black''']);
end

추가 답변 (1개)

C.J. Harris
C.J. Harris 2012년 7월 5일
편집: C.J. Harris 2012년 7월 5일
1. You cannot index into variables while using assignin. Therefore trying to assign the value 'red' to C(a,:) will not work.
2. The size of the char matrix C is a 3x3. So even if your use of assignin was correct you would get a size mismatch error when trying to assign the values of 'black' or 'blue' which are of length 5 and 4 respectively, and would not fit into your matrix.
You probably need to rethink the structure of your code. Do you really need to use assignin in this way?

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by