use app designer to assign values to variables in existing ".m" script

조회 수: 7 (최근 30일)
André_Santos
André_Santos 2019년 12월 1일
답변: Tejas 2024년 9월 25일
I have a complex script that calls many other scipts with alot of functions all in the same directory. one of these scripts is called data file, and stores all the necessary input variables for the main scirpt to work. I want to create GUI for the user to give this input, which is quite simple, just some values. And i want them to be stored in the correct variables inside this Data file. Should be simple enough, but i cant find the answer to this problem
  댓글 수: 1
Sam Blake
Sam Blake 2023년 8월 9일
Hello, did you ever find an answer to this question? I am having a similar issue at the moment. Any documentation for steps in the right direction would be much appreciated.

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

답변 (1개)

Tejas
Tejas 2024년 9월 25일
Hello André,
User inputs can be saved to a .M file by utilizing a button push callback along with file handling operations to open and write into the file. More details on writing to a file can be found in this documentation: https://www.mathworks.com/help/matlab/ref/fprintf.html#:~:text=a%20new%20line.-,Write%20Tabular%20Data%20to%20Text%20File,-Write%20a%20short .
Here is a simple example to illustrate the process:
  • Design a GUI in App Designer by adding two edit fields. Name one 'numVar' for numeric data and the other 'strVar' for text data.
  • Include a button in the GUI and assign a ButtonPushed callback to it. Within this callback, use the below code to write the variables to data file:
fid = fopen('dataFile.m', 'w');
fprintf(fid, 'numVar = %f;\n', app.numVarEditField.Value);
fprintf(fid, 'strVar = ''%s'';\n', app.strVarEditField.Value);

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by