MATLAB Answers

0

Can i call variable of another m file in GUI ?

Asked by Jay Patel on 17 Jul 2019
Latest activity Edited by Adam Danz
on 23 Jul 2019
Hello everyone,
i am designing a GUI for bellhop acoustic Toolbox, I m begineer in GUI designing. I wrote a program that can take few parameters as a input from user and run bellhop acoustic tool. I am wondering can i call variable of the another m file to GUI ? for example. If i want to take frequency as input parameter from user and put that user input frequency value to another m file and run bellhop when i press RunBellhop button ?
I am not sure how can i bind the variable of another m file to GUI ? Here I attach a screenshot of GUI and rough m file of GUI.
P.S. I am taking frequency input from user(GUI), writing that to another .m file and when i click on Run Bellhop button it should run below code
%=====================%
% Run Bellhop %
%=====================%
wbellhopenvfil('munkp',case_title,source_data,surface_data,ssp_data,bottom_data,options);
bellhop munkp
and plot the output in space provided :
%=====================%
% Reading output %
%=====================%
figure(2)
subplot(2,1,1)
counter = counter + 1 ; figure(counter)
plot( c , depths ), box on, grid on, view(0,-90)
xlabel('Sound speen (m/s)')
ylabel('Depth (m)')
title('Supply profile to bellhop')
subplot(2,1,2)
switch output_option
case '''C'''
plotshd( 'munkp.shd' ), box on, hold on
plot(rati, zati)
plot(rbty,zbty)
%plotssp( 'munkp' )
hold off
case '''E'''
plotray( 'munkp' ), box on, hold on
plot(rati, zati)
plot(rbty,zbty)
hold off
case '''R'''
plotray( 'munkp' ), box on, hold on
plot(rati, zati)
plot(rbty,zbty)
hold off
case '''IB'''
plotshd( 'munkp.shd' ), box on, hold on
plot(rati, zati)
plot(rbty,zbty)
%plotssp( 'munkp' )
hold off
otherwise
disp('Unknown output option.')
end
disp('done.')
Thanks in advance.
bellhopGUI.m

  4 Comments

Show 1 older comment
Thanks adam. I solved the problem using gui handle function, setappdata and getappdata.
%% stored data from GUI in callback
function freq_Callback(hObject, eventdata, handles)
newf = str2num(get(handles.freq,'String'));
setappdata(handles.freq,'mydata',newf);
end
%% storing done
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
load test3
%------------- New Frequency---------------%
X=sprintf('%d is the old frequency',freq);
disp(X)
val = getappdata(handles.freq,'mydata');
display(val)
freq = val;
display(freq)
bellhop_munk
Y=sprintf('%d is the new frequency',freq);
disp(Y)
%------------End New Frequency-------------%
Nice work! Here are some recommendations
  1. use str2double instead of str2num https://www.mathworks.com/matlabcentral/answers/52243-why-str2double-is-preferred-over-str2num
  2. Unless 'test3.mat' is changed during the use of the GUI, just import that data once and use persistent variables (see block of code below).
% *not tested
persistent freq X %any other variable in the file that's needed
if isempty(freq)
data = load('test3.mat','freq','X');
freq = data.freq;
X = data.X;
end

Sign in to comment.

0 Answers