MATLAB Answers

0

how can I save the value of a text area from app designer into a file .txt?

Asked by Gabriele Penna on 18 Dec 2018
Latest activity Answered by Kojiro Saito on 19 Dec 2018
I have to save the content of a text area into a specific folder as a file .txt.
I am able to do it in matlab but I can manage to pass the function in app designer.
this is the function I used in matlab for a similar issue (to pass a vector into a file.txt) :
function write_txt_file(vector,filename,directory)
%
%Function that writes a vector with NUMERICAL DATA to a file with a certain
%filename in a certain directory
%
% vector: vector/matrix variable containing the data to be written
% filename: name of file to be saved, i.e., [imagename '-LI.txt']
% directory: directory where the file should be saved
[r, c]=size(vector);
if ~isfolder(directory)
mkdir(directory)
end
if directory(end) == '/'
directory = directory(1:end-1);
end
if strcmp(filename(end-3:end),'.txt')
fp=fopen([directory '/' filename],'w');
else
fp=fopen([directory '/' filename '.txt'],'w');
end
if ~isempty(vector)
for i=1:r
for j=1:c
fprintf(fp,'%f ',vector(i,j));
end
fprintf(fp,'\r\n');
end
end
fclose(fp);
end

  0 Comments

Sign in to comment.

1 Answer

Answer by Kojiro Saito on 19 Dec 2018

Attached is a sample app designer file.
If button is pressed, a number written in text area will be save as a text file.
% Button pushed function: SaveButton
function SaveButtonPushed(app, event)
vec = str2double(app.TextArea.Value{:});
write_txt_file(vec, 'test', '.');
end
You can access to the characters by app.TextArea.Value and, in order to fit them to your write_text_file function, I've converted characters to double.

  0 Comments

Sign in to comment.