MATLAB GUI error when filling textboxes with data from file

1 回表示 (過去 30 日間)
Jack
Jack 2014 年 4 月 18 日
コメント済み: Jack 2014 年 4 月 21 日
I am writing a MATLAB GUI program where the data put in a textbox is stored in a .dat file. When the program is reloaded, the textboxes fetch the data from the file (so that the user doesn't have to re-input his info every time). This part of the code saves the data to a .dat file:
fid = fopen('textboxdata.dat', 'wt');
fprintf(fid, '%s\n', host);
fprintf(fid, '%s\n', username);
fprintf(fid, '%s\n', password);
fclose(fid);
[host, username, and password are strings]
This part of the code fetches the data and puts in into the text boxes:
s = dir('textboxdata.dat');
if s.bytes ~= 0
fid = fopen('textboxdata.dat', 'rt');
A = textscan(fid, '%s', 3);
A = A{1};
set(handles.hostbox, 'String', A(1))
set(handles.usernamebox, 'String', A(2))
set(handles.passwordbox, 'String', A(3))
fclose(fid);
end
Now this works fine, but when the user tries to use this data to log onto an FTP server, I get this error:
??? Error using ==> fprintf Function is not defined for 'cell' inputs.
Error in ==> realgui>logonbutton_Callback at 198 fprintf(fid, '%s\n', host);
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> realgui at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)realgui('logonbutton_Callback',hObject,eventdata,guidata(hObject))
But weirdly enough, if you retype all the data, you can log onto the FTP server just fine! I am thoroughly lost here. Does anyone have a clue what is happening?

採用された回答

Matt Tearle
Matt Tearle 2014 年 4 月 18 日
編集済み: Matt Tearle 2014 年 4 月 18 日
Try using cell indexing in these lines:
set(handles.hostbox, 'String', A(1))
set(handles.usernamebox, 'String', A(2))
set(handles.passwordbox, 'String', A(3))
That is, change to:
set(handles.hostbox, 'String', A{1})
set(handles.usernamebox, 'String', A{2})
set(handles.passwordbox, 'String', A{3})
My guess as to what's happening is: A is holding cell arrays of strings (even though it's just one string in each cell), so the String property of your edit boxes is holding a cell array, rather than a string (char array). So when you go to print the contents, fprintf is freaking out (cell array input rather than a string). Does that make sense?
  1 件のコメント
Jack
Jack 2014 年 4 月 21 日
Worked like a charm...thank you so much Matt!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by