MATLAB GUI - bring up a string - matlab

i' m try to create a simplex and fast gui on matlab. I made a button with a callback function, but I don' t know how to bring up a string that depends upon the result of the function. For example if the result of the function is 1, must appear the string "All ok!", if the result of the function is 0, must appear the string "It' s wrong!!!".

You can create a Static Text object or an inactive Edit Text object and modify the String property from your script with:
set(handles.edit_text, 'String', 'your_string')

An alternative to the static/edit uicontrol is to use a msgbox or an errordlg to display the result to the user depending on the result of your function.
if myFunction
msgbox ( 'All Ok' );
else
errordlg ( 'Error in Function', 'Error' )
end

Related

How to store order of user input and update string

I am trying to make the field 'all names' display all of the selected 'names' (letters in this case) in the order they have been selected (assumed left-to-right). What I have written seems to only be functional if only the first transformation is applied and for the case A + E/F/G. All the rest do not work and I do not understand why.
The code for the first dropdown is this:
% Value changed function: DropDown
function DropDownValueChanged(app, event)
app.FirstName = app.DropDown.Value;
switch app.FirstName
case 'A'
app.FinalNameDrop.Value='A';
case 'B'
app.FinalNameDrop.Value='B';
case 'C'
app.FinalNameDrop.Value='C';
end
end
I was advised by an internet stranger that I can "define a property on the class itself!" and so I tried
properties (Access = private)
Property % Description
FirstName
SecondName
end
However, I am unsure how this can help me. How would I go about making this functional?
just put this in all callback methods:
app.dropDownFinal.Value = sprintf('%s + %s + %s',app.dropDown.Value app.dropDown2.Value app.dropDown3.Value);

MATLAB : how to pass the string variable from a function to GUI?

For example, I have a function called function1 which has a variable, say str1 which holds a string.
I have also a GUI created by using MATLAB's GUIDE with the tag called textgui that has a static text tagged text1.
How do I pass the string str1 to the GUI so that when I run the GUI the static text text1 will show the string of the variable str1?
EDIT : function1 will generate the string str1 and then call the GUI textgui to show the string
EDIT2 : below is the code of function1 and an image of textgui.
str1 = 'some text here';
textgui; % runs the GUI
what should I do so that when I run function1, the static text in the GUI will show as the string which str1 holds?
Thanks for help!
When you set the value of str1, you just want to update the String property of the static text object using this variable.
str1 = 'my string';
set(handles.text1, 'String', str1)
If your function1 isn't a callback of your GUI, you can always use findall to find the handle to the static text object and set it that way.
str1 = 'my string';
set(findall(0, 'tag', 'text1'), 'string', str1)
If your function is initializing textgui, then you can pass the string directly to the GUI function as an input parameter:
str1 = 'my string';
textgui(str1);
This requires you to modify the textgui_OpeningFcn defined by GUIDE to process the input argument (stored in varargin):
function textgui_OpeningFcn(hObject, eventdata, handles, varargin)
set(handles.text1, 'String', varargin{1})
% Leave the other stuff that GUIDE sets alone
end

Problems Accessing Cell Array Items Within a GUI List - Matlab

I have a GUI handle list_h that is populated using a variable named tracks which is a [1xN] cell array. Since, each string within the list_h is assigned a value from 1:N I have been able to devise a for loop that can check which item has been selected from list_h, which works fine.
I was hoping I could extend upon this and be able to select an item from my list_h and be able to extract the string using the String property within list_h, but I am getting an error:
Undefined function 'eq' for input arguments of type 'cell'.
Error in guiPlay (line 13)
if i == list_value
Error while evaluating uicontrol Callback
I was also hoping I could get the full path to each item in list_h, since each item is a reference to an actual .wav file located on my hard drive? I figured this would be a case of concatenating the each item with its corresponding path if this is possible?
Here is the callback function that attempts to extract each list item's string:
function guiPlay(play_toggle_h, evt, list_h)
global predict_valence
button_value = get(play_toggle_h, 'Value');
list_value = set(list_h, 'Value');
N = length(predict_valence);
if button_value == 1
set(play_toggle_h, 'String', 'Pause');
for i=1:N
if i == list_value
track = get(list_h, 'String');
disp(track)
end
end
elseif button_value == 0
set(play_toggle_h, 'String', 'Play');
end
Just for debugging I have used the disp function to display the item.

Terminating while loop with a pushbutton Matlab

I have a uitable and a function that returns the id of the item once the user clicks on its respective row. The id is also a global variable, as it is used over various functions.
The idea is that I create an array with all the user chosen items from the uitable, terminated when save playlist is clicked. Ex. if the user chooses items with id's 5, 7, 9 in succession and then clicks the 'save playlist' button, I want the array to hold
5 7 9
I thought that the best way to do this is using a while loop. The while loop should check if the save button has been clicked. I have a global variable that changes once the program goes into the 'save playlist' button callback function.
The problem is that once the user chooses an ID, the array keeps on iterating until the user chooses another ID, and will keep on iterating etc etc until the save button is clicked. Ex: the user clicks on ID 5, the array will record 55555555555555... recurring until user clicks on ex. ID 7: 555555555555557777777777, since the program keeps on looping and looping without pausing.
The code looks like this:
while (keeprunning)
idvec = [idvec id];
end
keeprunning is a global variable and is initialized with a value of 1 in the opening function. It is changed to 0 in the 'save playlist' callback function.
I thought to fix this problem by introducing a new global variable call it 'itemselected' that initializes in the opening function as 1 and changes to 1 again everytime the cell selection callback is called. After an ID is added to the array in the while loop, this variable resets to 0.
The idea is that the loop should iterate while the 'save playlist' button is not clicked (controlled by the keeprunning global variable) BUT the program should not let the while loop iterate until the user clicks on another row i.e. until the cell selection call back function is called again.
I need something like this but this doesn't work (infinite loop). Or else some other suggestion??
while (keeprunning || trackselected)
idvec = [idvec id];
trackselected = 0;
end
Thank you in advance
To record a history of the user selecting fields inside a uitable, do something like this:
function tbl_example
% Create the figure
fh = figure('Visible', 'off')
ud.selected_list = [];
d = gallery('integerdata',100,[10 3],0);
t = uitable(fh,'Data',d,'ColumnWidth',{50}, 'cellselectioncallback', #cell_history );
set( fh, 'visible', 'on' );
set( fh, 'userdata', ud );
return
function cell_history( h, event_data )
fh = get( h, 'Parent' )
ud = get( fh, 'userdata' );
indx = event_data.Indices;
ud.selected_list = [ ud.selected_list h.Data( indx(1), indx(2) ) ]
set( fh, 'userdata', ud );
return
Then, when you hit your "playlist" button, just read the userdata field in the parent figure handle.
I fixed this problem after stopping from thinking about it and doing something else; fascinatingly it came to me as a very simple method.
What I did was create a global array in the CellSelectionCallback. The program checks whether the program was initiated to create a playlist through the use of a boolean variable in the 'create a playlist' button callback, and in this case the global array is appended with the chosen row's item's id. Since the array is global it can be used over different functions. The array is flushed after 'save playlist' button is clicked.
It was as simple as that, however, sometimes simple is not that simple, especially after about 10 hours of seeing matlab code.

routine to either evaluate code or transmit via udp

I have a set of code that, depending on how the program is initiated, will either be executed locally or sent to a remote machine for execution. The ideal way I imagine this could work would look something like the following:
line_of_code = 'do_something_or_other();';
if execute_remotely
send_via_udp(line_of_code);
else
eval(line_of_code);
end
The thing is, I know that the eval() function is ridiculously inefficient. On the other hand, if I write out line_of_code in each section of the if block, that opens the door for errors. Is there any other way that I can do this more efficiently than by simply using eval()?
EDIT: After more consideration and some discussion in the comments, I have my doubts that function handles can be transmitted via UDP. I'm therefore updating my answer, instead suggesting the use of the function FUNC2STR to convert the function handle to a string for transmission, then using the function STR2FUNC to convert it back to a function handle again after transmission...
To get around using EVAL, you can use a function handle instead of storing the line of code to be executed in a string:
fcnToEvaluate = #do_something_or_other; %# Get a handle to the function
if execute_remotely
fcnString = func2str(fcnToEvaluate); %# Construct a function name string
%# from the function handle
send_via_udp(fcnString); %# Pass the function name string
else
fcnToEvaluate(); %# Evaluate the function
end
The above assumes that the function do_something_or_other already exists. You can then do something like the following on the remote system:
fcnString = receive_via_udp(); %# Get the function name string
fcnToEvaluate = str2func(fcnString); %# Construct a function handle from
%# the function name string
fcnToEvaluate(); %# Evaluate the function
As long as the code (i.e. m-file) for the function do_something_or_other exists on both the local and remote systems, I think this should work. Note that you could also use FEVAL to evaluate the function name string instead of converting it to a function handle first.
If you need to create a function on the fly, you can initialize fcnToEvaluate as an anonymous function in your code:
fcnToEvaluate = #() disp('Hello World!'); %# Create an anonymous function
And the code to send, receive, and evaluate this should be the same as above.
If you have arguments to pass to your function as well, you can place the function handle and input arguments into a cell array. For example:
fcnToEvaluate = #(x,y) x+y; %# An anonymous function to add 2 values
inArg1 = 2; %# First input argument
inArg2 = 5; %# Second input argument
cellArray = {fcnToEvaluate inArg1 inArg2}; %# Create a cell array
if execute_remotely
cellArray{1} = func2str(cellArray{1}); %# Construct a function name string
%# from the function handle
send_via_udp(cellArray); %# Pass the cell array
else
cellArray{1}(cellArray{2:end}); %# Evaluate the function with the inputs
end
In this case, the code for send_via_udp may have to break the cell array up and send each cell separately. When received, the function name string will again have to be converted back to a function handle using STR2FUNC.