How to transfer a string input from GUI to another matlab file for processing

2 ビュー (過去 30 日間)
Noor
Noor 2017 年 1 月 26 日
編集済み: Niels 2017 年 1 月 27 日
Hi there, This is the first time for me here! I've been having some trouble with a code I am writing. I want to start processing large data and for that reason I decided to make a GUI, this is the first time for me to utilize the GUI function. so simply I want to plug the data into the GUI, and press different buttons for different types of processing. The GUI input data is two strings, seq_1 and seq_2, and simply by pressing a pushbutton I should get different types of processing with this data. The data should be kept as a string and sent over to be processed by the other matlab files.
This is my code: ------------------------------------------------------------------------------
Fig=figure
SEQ_1 = uicontrol(Fig,'Style','edit',...
'String','Enter Sequence 1 here (without spaces)',...
'Max',2,'Min',0,...
'Position',[30 380 1200 200], 'Callback',{@SEQ_1_Callback});
SEQ_2 = uicontrol(Fig,'Style','edit',...
'String','Enter Sequence 2 here (without spaces)',...
'Max',2,'Min',0,...
'Position',[30 100 1200 200],'Callback',{@SEQ_2_Callback});
PB1 = uicontrol(Fig,'Style','pushbutton','String','aa arrangement',...
'Position',[30 25 100 40],'Callback',{@PB1_callback});
PB2 = uicontrol(Fig,'Style','pushbutton','String','Hydrophobicity',...
'Position',[430 25 100 40],'Callback',{@PB2_callback});
PB3 = uicontrol(Fig,'Style','pushbutton','String','Polarity',...
'Position',[830 25 100 40],'Callback',{@PB3_callback});
function PB1_callback(source,eventdata)
run ('Seq_conversion.m');
end
function PB2_callback(source,eventdata)
run ('Seq_Hydrophobicity_characteristics.m');
end
function PB3_callback(source,eventdata)
run ('Seq_polarity_characteristics.m');
end
function SEQ_1_Callback(source,eventdata)
seq_1=get(SEQ_1, 'String');
save('Sequence_1', 'seq_1');
end
function SEQ_2_Callback(source,eventdata)
seq_2= get(SEQ_2, 'String');
save('Sequence_2', 'seq_1');
end
------------------------------------------------------------------------------
seq_2= get(SEQ_2, 'String');
save('Sequence_2', 'seq_1');
The above does not work, unless I manually write it in the command window
Seq_1 and Seq_2 are the two primary data I am interested in transferring for processing.
I even tried to put the other processing files under the pushbutton function, but the string is not transferring. I have no clue what I am doing wrong. I hope someone can enlighten me and help me get this code to work.
Thanks in advance!
  1 件のコメント
Adam
Adam 2017 年 1 月 27 日
Comment from Noor, moved from Answer:
Hi there , Well thanks for the response. Sorry I have most probably mistaken the seq_1 and seq_2, due to the many trials I was just running to get at least one of them to work.
I have previously tried the declaration, it still does not do anything. if I declare a string vector (seq_1 and seq_2), when I run the program. The string doesn't transfer into the declared vectors (seq_1 and seq_2), they stay empty. only if I manually write "seq_1=get(SEQ_1, 'String');" in the command prompt does the value transfer into seq_1, likewise for seq_2. I just tried again and declared, and below is the error message I get:
Undefined function or variable 'SEQ_2'.
Error in test_GUI>SEQ_2_Callback (line 43) seq_2= get(SEQ_2, 'String');
Error while evaluating UIControl Callback

サインインしてコメントする。

回答 (4 件)

Niels
Niels 2017 年 1 月 27 日
The code can not work because you try to save a variable -> seq_1 that has not been declared. If you name it seq_2 then maybe try to save seq_2 and not seq_1 + if you get an errormessage it would be helpfull to add it...

Noor
Noor 2017 年 1 月 27 日
編集済み: Noor 2017 年 1 月 27 日
Hi there , Well thanks for the response. Sorry I have most probably mistaken the seq_1 and seq_2, due to the many trials I was just running to get at least one of them to work.
I have previously tried the declaration, it still does not do anything. if I declare a string vector (seq_1 and seq_2), when I run the program. The string doesn't transfer into the declared vectors (seq_1 and seq_2), they stay empty. only if I manually write "seq_1=get(SEQ_1, 'String');" in the command prompt does the value transfer into seq_1, likewise for seq_2. I just tried again and declared, and below is the error message I get:
Undefined function or variable 'SEQ_2'.
Error in test_GUI>SEQ_2_Callback (line 43) seq_2= get(SEQ_2, 'String');
Error while evaluating UIControl Callback

Adam
Adam 2017 年 1 月 27 日
You have two options (well, at least).
One is to use the 'source' variable instead of SEQ_2 because that is what it represents in that case - i.e.
seq_2= get(source, 'String');
The other is to make your callback a nested function. I assume it isn't at the moment though I can't tell as we don't have your full file content, but to be nested it needs to sit inside the 'end' of the main function. Then it will have access to the workspace of the main function, including the SEQ_2 variable.
  3 件のコメント
Adam
Adam 2017 年 1 月 27 日
Gosh, I'd never noticed the guihandles function before! I guess I used to always use GUIDE for my UIs though and then when I started programmatic ones I did them in classes to never needed to look for such a function. Useful though.
Niels
Niels 2017 年 1 月 27 日
編集済み: Niels 2017 年 1 月 27 日
But its better to do as jan simon said since guihandles seems not to add every uicontrol to the structure (static txt wont be added). But its pretty easy to save them in a structure from the start and the result is "the same". Maybe i am mistaken but i dont think so. Was my latest experience
Edit: I probably just forgot the tag of the static txt.

サインインしてコメントする。


Jan
Jan 2017 年 1 月 27 日
Another way is to store the handles persistently in the figure:
function MainGUI
H.Fig = figure;
H.SEQ_1 = uicontrol(Fig,'Style','edit',...
... );
...
guidata(H.Fig, H);
end
function SEQ_1_Callback(source,eventdata)
H = guidata(source);
seq_1 = get(H.SEQ_1, 'String');
end
Instead of guidata the command setappdata and set(H.Fig, 'UsersData') would be sufficient also.
In this example the shown procedure is not really needed, because "source" contains the needed handle already. But if you need to access other GUI elements also, collecting the handles helps.

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by