How to check the lenght of the characters in a file and how to save it using GUI

1 回表示 (過去 30 日間)
Kaavya N
Kaavya N 2021 年 6 月 11 日
編集済み: Adam Danz 2021 年 6 月 12 日
I have read a .txt file in GUI , in another function I want to check if the file has less than 33 characters or not ,If it is less than 33 I want to save the file
function file_Callback(hObject, eventdata, handles)
[Filename,Pathname] = uigetfile('*.txt','Select Text File');
if isequal(Filename,0)
disp('User selected Cancel')
else
handles.f_id=fopen(Filename,'r');
handles.filename_txt=strcat(Pathname,Filename);
guidata(hObject,handles);
set(handles.text_path,'String',handles.filename_txt);
dr=dir(handles.filename_txt); size=num2str(dr.bytes);
filesize=strcat(size,' bytes');
set(handles.text_size,'String',filesize);
end
function Encode_Callback(hObject, eventdata, handles)
if length(text_path)<33
handles.textLabel = sprintf('Message to short');
set(textLabel, 'String', textLabel);
return;
end
end
How to pass the entire file and check its length in the Encode_Callback is less than 33

採用された回答

Adam Danz
Adam Danz 2021 年 6 月 11 日
編集済み: Adam Danz 2021 年 6 月 11 日
Quite a bit of guess-work going on here regarding your GUI and what it's doing but, ... (see comments)
function file_Callback(hObject, eventdata, handles)
[Filename,Pathname] = uigetfile('*.txt','Select Text File');
if isequal(Filename,0)
disp('User selected Cancel')
else
handles.filename_txt = fullfile(Pathname, Filename); % use fullfile() to combine path and filename
set(handles.text_path,'String',handles.filename_txt);
fr = fileread(handles.filename_txt); % fileread is easier to implement and meets your needs
handles.filesize = numel(fr); % simply count characters (numeric output) not bytes
set(handles.text_size,'String',num2str(handles.filesize)); % Convert number -> char
guidata(hObject,handles); % Update handles at the end
end
function Encode_Callback(hObject, eventdata, handles)
if handles.filesize < 33 % no need for length()
handles.textLabel = sprintf('Message to short');
set(textLabel, 'String', textLabel); % ??????? no idea what this is/does
return;
end
end
end
  7 件のコメント
Kaavya N
Kaavya N 2021 年 6 月 12 日
I want to perform encoding on the contents of the file , so if I give path of the file path and name that i selected in file_callback will all the contents of the file be passed
Adam Danz
Adam Danz 2021 年 6 月 12 日
編集済み: Adam Danz 2021 年 6 月 12 日
Ah, so you want to pass the file content. I suggest you pass the file path/name as shown in my previous comment and then read-in the file within the function.
My answer uses fileread to read the file and that returns one long character array of the entire file. You may need to read the file differently depending on how you plan to analyze it.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by