Clear Filters
Clear Filters

standalone(EXE) textscan is not working well

1 view (last 30 days)
Hello. I want to make simple code as standalone(EXE).
1. Read text file
2. Sorting them
3. Save them to mat, txt or csv
Reading textfile works well in matlab. but whenever i deploy this to standalone, reading textfile returns broken data.
I made standalone using (MATLAB-APPS-Application Compiler) with same code.
filename = dir('Measurement*.asc');
copyfile(filename.name, [filename.name(1:end-4) '.txt']);
filename = fullfile(pwd, [filename.name(1:end-4) '.txt']);
fid = fopen(filename, 'r+');
rawdata1 = textscan(fid, '%s', 'Delimiter', '\n', 'MultipleDelimsAsOne', 1, 'headerlines', 3, 'TextType', 'string');
fclose(fid);
rawdata2 = readmatrix(filename, 'Numheaderlines', 3, 'ConsecutiveDelimitersRule', 'join', 'delimiter', ' ', 'OutputType', 'string');
save('TEST.mat');
Both rawdata1 and rawdata2 returns broken data... please help me..

Answers (1)

Walter Roberson
Walter Roberson on 5 Jan 2024
When you are using compiled executables, it is especially important to use fully qualified filenames everywhere.
filename = dir('Measurement*.asc');
No, you need to be specifying the directory name. See ctfroot or otherwise figure out where to read files from.
The default directory for files is not the user's "current" directory -- the program doesn't have any idea which directory is the "current" directory.
copyfile(filename.name, [filename.name(1:end-4) '.txt']);
Fully qualify!
oldname = fullfile(filename(1).folder, filename(1).name);
[olddir, basename] = fileparts(oldname);
newname = fullfile(olddir, [basename ".txt"]);
copyfile(oldname, newname);
  5 Comments
Walter Roberson
Walter Roberson on 5 Jan 2024
Getting the file names wrong can cause the problems you are seeing.
It is not worth debugging any further until after the filenames have been fixed to use absolute file names.
WoongLae Cho
WoongLae Cho on 5 Jan 2024
There is a part saving variables to file.
when i open it, the filename is fine, it is in the proper path.
and i tried below code to give EXE proper path. but getting same wrong textscan data.
if isdeployed
[status, result] = system('path');
installpath = char(regexpi(result, 'Path=(.*?);', 'tokens', 'once'));
else
installpath = pwd;
end
filename = dir('Measurement*.asc');
copyfile(fullfile(installpath, filename.name), [fullfile(installpath, filename.name(1:end-4)) '.txt']);
filename = fullfile(installpath, [filename.name(1:end-4) '.txt']);

Sign in to comment.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!