I want to record gui data into text file using switch case

2 Ansichten (letzte 30 Tage)
cansu
cansu am 8 Feb. 2019
Verschoben: Sabin am 13 Dez. 2024
I want to write answers of questionnare ,which i have been designed in GUI with group of radio buttons, into a text file which is called DATASET.txt .Every button groups have 9 answer corresponding 9 radio buttons. For example; if the user selects Valance1 (that is Tag) that will be written in the text file 1.
i is the user id and must increase one by one.
The output in the text file may be like that:
i V A L D
1 6 5 9 3
2 1 2 3 5
3 2 5 3 5
4 6 2 6 5
That is my code;
Valance_SelectionChangedFcn(hObject, eventdata, handles)
switch get(get(handles.Valance,'SelectedObject'),'Tag')
case 'Valance1'
V=1;
case 'Valance2'
V=2;
case 'Valance3'
V=3;
case 'Valance4'
V=4;
case 'Valance5'
V=5;
case 'Valance6'
V=6;
case 'Valance7'
V=7;
case 'Valance8'
V=8;
case 'Valance9'
V=9;
end
Arousal_SelectionChangedFcn(hObject, eventdata, handles)
switch get(get(handles.Arousal,'SelectedObject'),'Tag')
case 'Arousal1'
A=1;
case 'Arousal2'
A=2;
case 'Arousal3'
A=3;
case 'Arousal4'
A=4;
case 'Arousal5'
A=5;
case 'Arousal6'
A=6;
case 'Arousal7'
A=7;
case 'Arousal8'
A=8;
case 'Arousal9'
A=9;
end
Dominance_SelectionChangedFcn(hObject, eventdata, handles)
switch get(get(handles.Dominance,'SelectedObject'),'Tag')
case 'Dominance1'
D=1;
case 'Dominance2'
D=2;
case 'Dominance3'
D=3;
case 'Dominance4'
D=4;
case 'Dominance5'
D=5;
case 'Dominance6'
D=6;
case 'Dominance7'
D=7;
case 'Dominance8'
D=8;
case 'Dominance9'
D=9;
end
Liking_SelectionChangedFcn(hObject, eventdata, handles)
switch get(get(handles.Liking,'SelectedObject'),'Tag')
case 'Liking1'
L=1;
case 'Liking2'
L=2;
case 'Liking3'
L=3;
case 'Liking4'
L=4;
case 'Liking5'
L=5;
case 'Liking6'
L=6;
case 'Liking7'
L=7;
case 'Liking8'
L=8;
case 'Liking9'
L=9;
end
X = [i; V; A; D; L];
fileID = fopen('DATASES.txt','w');
fprintf(fileID,'%s %s %s %s %s\n','i','V','A','D','L');
fprintf(fileID,'%d %d %d %d %d\n',i, V, A, D, L);
fclose(fileID);
But that code was error. According to warning; V A D L aren't defined variables. By the way beginning of code that was't appear in that context i defined i (user id) .
So can anyone help me :((

Antworten (1)

Adam
Adam am 8 Feb. 2019
Verschoben: Sabin am 13 Dez. 2024
Your 'otherwise' blocks are empty and you don't create a default variable first so it is possible that each of those variables may never get defined.
surely if your options are tagged so clearly though you can just do a simple char manipulation instead of all these switch cases.
e.g.
likingOption = get(get(handles.Liking,'SelectedObject'),'Tag');
L = likingOption( end );
  2 Kommentare
Rik
Rik am 9 Feb. 2019
Verschoben: Sabin am 13 Dez. 2024
49 is the char value of 1. You can easily adapt the code Adam suggested.
likingOption = get(get(handles.Liking,'SelectedObject'),'Tag');
L = likingOption( end ) - '0';
Any value outside the range you expect will mean that the Tag has a value you didn't expect.
cansu
cansu am 9 Feb. 2019
Verschoben: Sabin am 13 Dez. 2024
Thanks guys @Rik , @Adam !! It works as you said :)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by