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

3 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by