Outputting imported files in Matlab App Designer

5 views (last 30 days)
I have created a file import dialogue from on my app Designer. The file I have imported are generally .txt files which I need to do two tasks with.
Firstly, I need to read the output (the imported file) and save it in its .txt format.
Secondly I would like to import the same into Matlab as a table and save the same as such.
Below is the code that I have managed to write on the same
value = app.Select_DLRDropDown.Value;
[file,path]=uigetfile;
DLR_siteA=fullfile(path,file);
site_A =DLR_siteA; % the app runs succesfully upto this point when I press the Select_DLR button but it does not open the file site_A
open("site_A"); % returns 'The variable site_A does not exist'.
How can I be able to save the file that has been imported by this tool? And is it possible to convert it directly to a Mat table first then output it as a table?
Any support is appreciated.

Accepted Answer

Guillaume
Guillaume on 10 May 2019
At no point does your code attempt to import anything.
DLR_siteA=fullfile(path,file);
Ok, this store the full path of the file selected in the DLR_siteA variable.
site_A = DLR_siteA;
As always in matlab, when you do A = B, you copy the content of the B variable into the A variable. In your case, B (DLR_siteA) is the full path of the file, so you've just copied the full path of the file into variable site_A
open("site_A");
As documented, open opens a file in the appropriate application. On windows a text file, will typically be opened in notepad. open does not do import. In any case, what you're passing to open is the filename "site_A" (I'd wager that file doesn't exist), not the content of the site_A variable.
Assuming the file you want to import is a text file. There are many ways you can use. Which one is more appropriate depends on the format of your text file. So if you want more help attach an example file. For well formatted text files, the simplest way to import them is with readtable which would give you the table you desire.
I'm a bit confused by that sentence: I need to read the output (the imported file) and save it in its .txt format. The output of what? And why would you save back the file in the same format it started as?
  9 Comments
Lui
Lui on 18 May 2019
Thank you all for the guidance. I realized I had not specified any file in my initial code. so for any other person in the same problem I did the following for the button Load
% Button pushed function: LoadM2002Button
function LoadM2002ButtonPushed(app, event)
%load file1
[filename, pathname] = uigetfile('*.txt','Please select txt files containing project code info...','MultiSelect','off');
Data=readtable([pathname,filename]);
Then afterwards I did modify my data as desrired
% Retrieve the table
Data=Data(2:end,1:40);
Data=table2array(Data(:,:)); % needed the data as a matruix
finally I strored it in my working directory
[file,path] = uiputfile('*.MAT','Please select or provide .MAT file filename for extracted project code info...');
writematrix(Data,'DataA');
That worked. But the file I got was not a Matlab file. It was a text file. So any help will still be appreciated.
Guillaume
Guillaume on 18 May 2019
Edited: Guillaume on 18 May 2019
1) You ask the user for a file name and path for the mat file, then completely ignore it and use DataA as the file name.
2) The function to save data as mat file is save. Indeed writematrix only writes text or excel files.
So:
save(fullfile(path, file), 'Data'); %Save Data in the mat file specified by the user.

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!