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

조회 수: 1 (최근 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