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
채택된 답변
Walter Roberson
2019년 12월 5일
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
