editing a string using GUI editable text/popup within guide
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I have a string, where I use strrep to replace a variable witha user input:
string = ('Today, I ate some (x)')
user_input = input('Enter your food: ', 's');
newstring = strrep('string', '(x)', user_input);
I want to use an editable textbox to get user_input. How would I do that?
My full script will have 3 editable textboxes, and 2 popups.
the popups will be changing a directory path, like I do here with sprintf:
user_input_food = input('enter food: ');
user_input_amount = input('enter amount: ');
root = 'C:\Users\Administrator';
% full path should look like C:\Users\Administrator\food\amount\food_amount.txt
filepath = fullfile(root, sprintf('food%s', user_input_food,), user_input_amount, sprintf('food%s_amount%s.txt', user_input_food, user_input_amount);
how do I get a popup menu to populate the user_input_experiment/user_input_squad variables?
2 件のコメント
GUIDE or APp Designer ?
using guide, good point
採用された回答
user_input_food = handles.food_textbox.String; %assuming you named the edit uicontrol food_textbox
user_input_amount = handles.amount_textbox.String;
filepath = fullfile(root, sprintf('food%s', user_input_food), user_input_amount, sprintf('food%s_amount%s.txt', user_input_food, user_input_amount));
how do I get a popup menu to populate the user_input_experiment/user_input_squad variables?
If it is to be done according to computed content, then set the String property of the popup to a cell array of character vectors, one per line. Access the Value property to determine the index of which was selected. If you want to know the selected text, then for example,
experiment_choices = handles.experiment_popup.String;
user_choice = handles.experiment_popup.Value;
user_input_experiment = experiment_choices{user_choice};
12 件のコメント
avram alter
2019 年 12 月 5 日
編集済み: avram alter
2019 年 12 月 5 日
I am getting this error:
Error using importdata (line 139)
Unable to open file.
Error in Macro_updating>Macro_updating_OutputFcn (line 87)
Mac_Templ = importdata(filepath);
Error in gui_mainfcn (line 264)
feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
Error in Macro_updating (line 42)
gui_mainfcn(gui_State, varargin{:});
I am trying to use the GUI to access a certain file, but the script runs, and runs in to the problem of not having the proper variables. How do I get the script to wait for a user input, before running the importdata() function?
I tried using pause, but then the script doesn't continue at all.
If you think it will help, I can post the full code with an explanation.
How do I get the script to wait for a user input, before running the importdata() function?
if isempty(user_input_food) || isempty(user_input_amount) || isempty(user_input_experiment) || isempty(user_input_squad)
return
end
%now proceed to the importdata()
if ~exist(filepath, 'file')
errordlg( sprintf('Opps, input file does not exist: "%s"', filepath), 'Error Dialog', 'modal')
return;
end
Mac_Templ = importdata(filepath);
avram alter
2019 年 12 月 9 日
編集済み: avram alter
2019 年 12 月 9 日
I did that, and immediately got the dialog box telling me that the file does not exist, with the placeholder values I set in the GUI. When commenting out that line, I got the same error as last time. I am appending the full code, to clarify.
function varargout = Macro_updating_OutputFcn(hObject, eventdata, handles)
% Initialize by cleaning up
%add complete paths to every template file you will be using, if not, MATLAB cannot find the appropriate files
addpath('C:\Users\Administrator\Dropbox (********)\******** Team Folder\Matlab\RFID chip reader\EXPT1\1',...
'C:\Users\Administrator\Dropbox (********)\******** Team Folder\Matlab\RFID chip reader\EXPT1\2',...
'C:\Users\Administrator\Dropbox (********)\******** Team Folder\Matlab\RFID chip reader\EXPT1\3',...
'C:\Users\Administrator\Dropbox (********)\******** Team Folder\Matlab\RFID chip reader\EXPT1\4',...
'C:\Users\Administrator\Dropbox (********)\******** Team Folder\Matlab\RFID chip reader\EXPT2\1',...
'C:\Users\Administrator\Dropbox (********)\******** Team Folder\Matlab\RFID chip reader\EXPT2\2',...
'C:\Users\Administrator\Dropbox (********)\******** Team Folder\Matlab\RFID chip reader\EXPT2\3',...
'C:\Users\Administrator\Dropbox (********)\******** Team Folder\Matlab\RFID chip reader\EXPT2\4');
root = 'C:\Users\Administrator\Dropbox (********)\******** Team Folder\Matlab\RFID chip reader';
%ensure you are entering just the number, and nothing else, including
%spaces before or after, as this will throw an error.
experiment_choices = handles.popupmenu1.String;
user_choice = handles.popupmenu1.Value;
user_input_exp = experiment_choices{user_choice};
user_input_squad = handles.edit1.String;
filepath = fullfile(root, sprintf('EXPT%s', user_input_exp), user_input_squad, sprintf('EXP%s_SQ%s_Template.txt', user_input_exp, user_input_squad));
if isempty(user_input_exp)|| isempty(user_input_squad)
return
end
if ~exist(filepath, 'file')
errordlg(sprintf('file location does not exist, "%s"', filepath), 'Error Dialog', 'modal')
return
end
Mac_Templ = importdata(filepath);
user_input_stage = handles.edit2.String;
todaysMac = Mac_Templ;
todaysMac = strrep(todaysMac, '(m)', user_input_stage); %update stage with user input
user_input_session = handles.edit3.String;
todaysMac = strrep(todaysMac, '(n)', user_input_session); %update session with uyser input
user_input_list = handles.edit4.String;
todaysMac = strrep(todaysMac, '(x)', user_input_list); % update list with user input
todaysMac = string(todaysMac);
todaysMac = compose(todaysMac); % There was an issue with the files, where
% the output would be in a single line. this fixes it, by using the escape
% characters located at the end of each line in the template (\n), compose
% basically reads those as a command, placing each box into a new line.
fid = fopen(fullfile('C:\Users\Administrator\Dropbox (********)\******** Team Folder\Matlab\RFID chip reader\Completed_Macros', sprintf('EXP_%s_SQ%s_completed.mac', user_input_exp, user_input_squad)), 'wt'); % Makes a new file in
%specified directory, with specified name and format at the end. saving a
%second file with the same name will overwrite older file.
fprintf(fid, '%s', todaysMac);
fclose(fid);
disp('Done');
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
Macro_updating_OutputFcn
By convention, in GUIDE, an OutputFcn is not called until you return from the GUI. So to repair the problem: Don't return from the GUI until you are ready to.
The GUI is returned from under one of three circumstances:
- The figure is closed and any Close Request Fcn does not force it to continue existing; https://www.mathworks.com/help/matlab/ref/matlab.ui.figure-properties.html#buiwuyk-1-CloseRequestFcn ; Or
- The program specifically calls uiresume() to force returning even though the figure remains open. This is not at all common; Or
- the GUI has not been configured to wait for response, in which case it typically returns the handle of the GUI figure to the caller. This is common.
The way to configure the GUI to wait for figure close is to go into the OpenFcn callback and remove the commenting-out character % on the uiwait call. For example in one I had handy, the code by default looks like
% UIWAIT makes DOSEN wait for user response (see UIRESUME)
% uiwait(handles.figure1);
and that would change to
% UIWAIT makes DOSEN wait for user response (see UIRESUME)
uiwait(handles.figure1);
avram alter
2019 年 12 月 9 日
編集済み: avram alter
2019 年 12 月 9 日
so that helped, inasmuch I don't immediately get the error code. I am now getting
Error in Macro_updating>Macro_updating_OutputFcn (line 81)
experiment_choices = handles.popupmenu1.String;
Error in gui_mainfcn (line 264)
feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
Error in Macro_updating (line 42)
gui_mainfcn(gui_State, varargin{:});
I think that means that the handles property has changed? I am not sure
thanks for all the help so far, I really appreciate it.
Using dbstop if error, handles comes up as a 0x0 double, not a structure. I am not entirely sure if this is the problem, or how to fix it.
Can you attach your *.m and your .fig ?
there you go. other than some bits I censored for privacy reasons, that is the exact code.
You are clicking to close the figure. Once you do that, the figure no longer exists, and that means that the handles no longer exist, as the handles are attached to the figure.
You would be better off adding a button for the user to indicate that they want to proceed. Have the button do whatever checking is appropriate (e.g., ensure that none of the fields are empty), and if everything looks good, call uiresume() . Then in the CloseFcn, after you have retrieved the contents you want, close the figure.
I have never used uiresume() before. how would that be achieved?
I assume I would put the
Mac_Templ = importdata(filepath)
into a buttondown function.
would that be sufficient?
uiresume( ancestor(hObject, 'figure') )
into a buttondown function.
I do not recommend that. That risks the user accidentally triggering the function, such as if they click just outside an edit box because they are not being precise enough.
I recommend instead that you add a new button with a name such as 'Go' that does the work, moving all those things outside of the OutputFcn callback and into the Callback for the button. You would not use uiresume() if you did that. However, you might want to have the Go callback close the figure.
That's what I thought you meant. Populating the handles and then only actually running the importdata() at the user's behest. I will definitely try that, and update you. Thanks for the help so far, I really appreciate it.
that works perfectly, thanks for the help. If you have time, I posted a new question for a connected project. thanks so much.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Software Development Tools についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
