Saving the values into a mat file error

Hy Guys. I hope you all are well. I have an urgent question as I am working on a big project. I am having an error in a code and I just want to fix this small error. I am saving the values into a mat file with my GUI and then uploading the values back by clicking on the save and upload button respectively.
However, I want my edit text boxes to write only letters and not alphabets. So, I entered an additional code where I make the letters read only and output Enter a number if an alphabet is written. However, my code shown above is not working for saving the values as it is giving me the errors:
cell contents reference from a non-cell array object.
Error in GUI_ParametersFinal>edit_TemperatureValue_Callback (line 187)
evalstring = sprintf('handles.mystructdata.%s.string = ''%s''',tagname,str{1});
This is my complete code:
function edit_TemperatureValue_Callback(hObject, eventdata, handles)
% hObject handle to edit_TemperatureValue (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_TemperatureValue as text
% str2double(get(hObject,'String')) returns contents of edit_TemperatureValue as a double
str=get(hObject,'String');
tagname=get(hObject,'tag');
evalstring = sprintf('handles.mystructdata.%s.string = ''%s''',tagname,str{1});
evalc(evalstring);
guidata(hObject,handles)
For saving pushbutton, my code is
data = handles.mystructdata;
save('testmatfile.mat','data');
It will be so nice if someone can help :) Thank you so much.

 採用された回答

Stephen23
Stephen23 2019 年 10 月 16 日
編集済み: Stephen23 2019 年 10 月 16 日

0 投票

Rather than this indirect, complex, buggy, obfuscated, strongly inadvisable code:
evalstring = sprintf('handles.mystructdata.%s.string = ''%s''',tagname,str{1});
evalc(evalstring);
just use simpler, neater, efficient, and much better dynamic fieldnames:
handles.mystructdata.(tagname).string = str{1};
"cell contents reference from a non-cell array object."
Clearly str is not a cell array, so your indexing will throw an error. It is worth noting that some GUI objects (e.g. some uicontrol types) can support the String property as either a character vector or cell array of character vectors: it is up to you to ensure/check this in the code. You might just need:
handles.mystructdata.(tagname).string = str;
or you could do something fancy like this:
if iscellstr(str)
handles.mystructdata.(tagname).string = str{1};
else
handles.mystructdata.(tagname).string = str;
end

1 件のコメント

MHS
MHS 2019 年 10 月 16 日
Thank you so much for assistance. Yes, I got it. I will come back to you if I have any more problems :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

タグ

質問済み:

MHS
2019 年 10 月 16 日

編集済み:

MHS
2019 年 10 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by