GUI_1.get string from edit box 2.load .mat which has same name with string input

12 views (last 30 days)
HI. I'm designing a gui.
I'm using r2018a and guide.
I want to input a string, which is a person's name, into a edit1 box, and then display that string on the edit2 box.
And I finally want to load .mat file named the string that I entered in edit1 box.
I can input a string and display, but I got error message on load.
Error code said, ' load should be string arrays or character arrays.'
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)
user_name = get(handles.edit1, 'String');
set(handles.edit2, 'String', user_name)
user_data = load(user_name); %user_name is string type, right? what's the problem?
plot(handles.axis1, user_data)
Plz give me some advice. Appreciate in advance ;)

Accepted Answer

Geoff Hayes
Geoff Hayes on 9 Dec 2018
I've found that sometimes with code like
user_name = get(handles.edit1, 'String');
user_name is a cell array (with one string element) and so is not a character array. What you may have to do here is to convert this into a string with char as
user_data = load(char(user_name));
  1 Comment
MINKYUNG KIM
MINKYUNG KIM on 10 Dec 2018
Wow amazing Thanks I solved problem
user_data = load(char(user_name));
or
user_data = load(string(user_name));
It works well.
I'm a begginer so I always miss that kind of small detail.
Thank you for your advice

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!