I want the GUI to input a result from another m file in a static text box after clicking a push button.

조회 수: 1 (최근 30일)
I am trying to create a GUI using guide. I have a .m file which has a certain result.
power_difference = mean(adj_crank_power)-mean(trainer_power)
% Display the results using fprintf
if power_difference < -1
fprintf('The trainer over-estimates the power by %0.2fW.\n', abs(power_difference))
elseif power_difference > 1
fprintf('The trainer under-estimates the power by %0.2fW.\n', abs(power_difference))
else
fprintf('The trainer gives an accurate power reading compared to the crank.\n')
end
I want the fprintf result to appear in my static text box after clicking a push button

채택된 답변

Voss
Voss 2022년 12월 15일
As I understand the situation, you have an m-file that contains the code you posted, and you want to run that m-file from your GUI and have the message printed by the m-file to the command line appear instead in a static text box in your GUI. Is that right?
If so, modify the m-file to return a character vector or string (make this optional, if you need to preserve the existing behavior of the m-file). Something like:
function out = get_power_message(adj_crank_power,trainer_power)
power_difference = mean(adj_crank_power)-mean(trainer_power);
if power_difference < -1
out = sprintf('The trainer over-estimates the power by %0.2fW.', abs(power_difference));
elseif power_difference > 1
out = sprintf('The trainer under-estimates the power by %0.2fW.', abs(power_difference));
else
out = sprintf('The trainer gives an accurate power reading compared to the crank.');
end
Then call the function in your GUI with the appropriate inputs, and set the 'String' of your static text box to the result.
% for example:
msg = get_power_message(adj_crank_power,trainer_power);
set(handles.text_box,'String',msg);
  댓글 수: 2
Gian Paulo
Gian Paulo 2022년 12월 15일
when I tried that I got an error which says "Function definitions in a script must appear at the end of a file"
Voss
Voss 2022년 12월 15일
편집: Voss 2022년 12월 15일
Try moving the function to the end of the file.
How do you have code for a GUIDE GUI in a script? Maybe you should upload the relevant files, using the paperclip icon.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2022년 12월 15일
function msg = YourFcn(your inputs)
power_difference = mean(adj_crank_power)-mean(trainer_power);
if power_difference < -1
msg = sprintf('The trainer over-estimates the power by %0.2fW.', abs(power_difference));
elseif power_difference > 1
msg = sprintf('The trainer under-estimates the power by %0.2fW.', abs(power_difference));
else
msg = sprintf('The trainer gives an accurate power reading compared to the crank.');
end
end
Call this from inside the callback of the pushbutton
function pushbuttonCallback(ObjectH, EventData, handles)
msg = YourFcn(the inputs); % No idea, where the inputs come from!
set(handles.staticText.String, msg); % Insert the name of the handle
end

카테고리

Help CenterFile Exchange에서 Transform Data에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by