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

3 vues (au cours des 30 derniers jours)
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

Réponse acceptée

Adam Danz
Adam Danz le 11 Juin 2021
Modifié(e) : Adam Danz le 11 Juin 2021
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 commentaires
Kaavya N
Kaavya N le 12 Juin 2021
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 le 12 Juin 2021
Modifié(e) : Adam Danz le 12 Juin 2021
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by