Question

Premise

I am creating a MATLAB GUI using GUIDE. I have roughly 10 "edit text" boxes and roughly 10 "static text" boxes. The purpose of the program is to take the data from the "edit text" boxes, perform a bunch of calculations, and then output the results to the "static text" boxes.

Problem

My problem is that a have a function refresh_data() that needs to access the handles for each of the 20 objects. So right now I'm passing all the handles into the function, which looks something like this...

refresh_data(handles.edittext1, handles.edittext2, ... handles.statictext10)

The function refresh_data is contained within a separate .m file. Is there a way of passing all of my handles at once?

Feel free to suggest other methods of going about this, I am rather new to MATLAB GUI's.

Was it helpful?

Solution

As the comments suggested, you can pass the entire handles structure at once:

function refresh_data(handles)
  temp = get(handles.edittext1, 'String')
  % convert temp to number, process, convert back to string
  set(handles.statictext1, 'String', temp)
end

Since you're doing a bunch of these, you can loop it using Matlab's dynamic field names:

for k = 1:20
    box_to_get = ['edittext' k];
    box_to_set = ['statictext' k];
    temp = get(handles.(box_to_get), 'String');
    % processing
    set(handles.(box_to_set), 'String', temp);
end

See http://www.mathworks.com/help/matlab/matlab_prog/generate-field-names-from-variables.html for more information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top