How do I prompt the user to choose a file that will be loaded in matlab? - matlab

I want the user of a script I'm writing to be able to navigate to a file containing their data and to load the data into the workspace. For example, if a csv file contains two cells with the values 1 and 2 respectively, I want the user to simply choose this file and those two values will be assigned to a variable in the workspace.
I have looked at using:
filename = uigetfile('*.xlsx','*.csv')
But that just returns the name of the file. Perhaps I could construct a full path to where the file they choose is found, and then read it in that way (using xlsread or csvread) but I think I'm probably missing something here. It seems that there should be a more straightforward way of doing it.

I believe that you're looking for the uiopen() function. This function will:
Open dialog box for selecting files to load into workspace.
On default, this function will display in a file explorer dialog box with the filter set to all MATLABĀ® files (with file extensions *.m, *.mlx, *.mat, *.fig, *.mdl, and *.slx).
However, you can import data from data files like CSV files and spreadsheets as well. Simply select the (All Files) option for the Files of Type field.
Once you've selected the data file you're interested in, you will be prompted with another GUI object that previews the data you are about to load into MATLAB's workspace. If you're satisfied with the format of the variables presented in the preview, simply hit the green check-mark at the right-side of the tool-box ribbon in the GUI object and, huzzah, all of the data file's contents have been loaded into separate variables (named according to their respective headers).
Alternatively, though this is undeniably a longer-winded and uglier approach, if you'd like to use the filename returned from uigetfile('*.xlsx', '*.csv'), you could use the importdata() function. This will output a struct that contains each of the variables from your data file as a separate field:
[filename, pathname] = uigetfile( ...
{'*.csv;', 'CSV file (*.csv)';
'*.xlsx', 'Excel Spreadsheet file (*.xlsx)'; ...
'*.*', 'All Files (*.*)'}, 'Pick a File to Import');
full_filename = fullfile(pathname, filename);
[A, delimiterOut] = importdata(full_filename);

Related

Uploading multiple .txt files at once on MATLAB GUI

I would like some guidance on how to import multiple .txt files containing data seperated by comma on a MATLAB GUI. Once the files are uploaded, I have a function that will manipulate all the data from each .txt file.
Any help is appreciated.
The easiest way to import multiple files is to:
Use the file importer GUI in matlab and generate a script after you
selected your preferred parameters
Generate a script (there is a button to generate a script in the importer)
Modify the script with a for loop to load multiple files and save them in a variable (a cell array can handle different sizes of data in each file)
Try uigetfile to launch a dialog for loading files. Set 'MultiSelect' to 'on' in order to select multiple files at once.
Here's an example call:
[filenames, pathname] = uigetfile({'*.txt; *.csv','Comma separated values';...
'*.*','All files'},'Select files','MultiSelect','on');
You will need to check if the user actually selected a file or if they canceled.
If I understand your question correctly, you have a GUI already. In this case you just need to add the above call to your designated callback function (i.e. whatever you click to invoke this file load interface).

MATLAB script that can call .mat files without directory

I have a MATLAB script and a lot of folders that are called in that script. I am sending out the script, which is part of a publication and would like to make the script freely accessible along with the .mat files. I was wondering if there was an easy way to do this where the user can just run the script and the files can be called from the script. So it's like a software that just calls the .mat files rather than a code that the user needs to read and understand to call the files.
Thanks!
You have a couple of options.
Determine the directory dynamically and use that to load the .mat files (preferred)
thisdir = fileparts(mfilename('fullpath'));
matpath = fullfile(thisdir, 'subdirectory', 'file.mat');
data = load(matpath);
Put the folder containing the .mat files on the PATH and then load them with just the name
addpath('/folder/containing/mat/files')
data = load('file.mat');
Have the user select the files using uigetfile
[fname, pname] = uigetfile();
filename = fullfile(pname, fname);
data = load(filename);

How to save the matlab workplace variable including original file name?

I would like to know how I can save the matlab output files (i.e. matlab workplace variables), by including original file name.
e.g. I open a file (filename.mat) with load filename.mat. Then I run a code to do calculation and I get some workplace variables (e.g. flow, pressure). I want to save those variables as filename_flow.mat and filename_pressure.mat.
I will use the same code on different filename, so I would like to know how I can save my variables as mentioned above (i.e. including the original file's name)?
FileToBeLoaded = 'filename.mat';
[pathstr,filename,ext] = fileparts(FileToBeLoaded)
load([filename ext]);
%// calculate stuff
FlowVariable = %// some calculation
save([filename '_flow'],FlowVariable)
The same of course works for other names as well. You pull apart the original file name to its actual name and extension, and use the original name, add something (_flow in this case) and save that. The default of MATLAB is already to save to a .mat file, so that's taken care of automatically.

Save or Load Data from/to GUIDE/Workspace

I'm new in GUIDE in MATLAB. I have two different problems:
I would like to save in a .mat file all variables (about 1000) from workspace using a button in GUI in MATLAB. How can I do?
I have a button where after press on it I'm able to load a specific .mat file from my path, always using GUI, but I would like that the variables contained in this file, became presents in the base workspace.
In other words, I have a button, "LOAD", in GUIDE in MATLAB where I can load a .mat file, and the variables contained in the .mat file must be loaded into the 'base' workspace when the button is clicked.
Please help me.
For your first question, I would suggest just putting a command like save('filename.mat'); in the "Save" button's callback. But what variables? If they are in the base workspace, see my answer to your second question below.
To load data into the base workspace, you might try the evalin command:
evalin('base','load(''filename.mat'')');
The 'base' argument tells it to run the command in the base workspace.
If your file name is in a variable:
fname = 'filename.mat';
evalin('base',['load(''' fname ''')']);
Alternatively, you could use sprintf:
loadCmd = sprintf('load(''%s'')',fname);
evalin('base',loadCmd);

Is there a way to prompt for multiple file select and assign those files (and paths) to a variable?

Is there a way to prompt for multiple file select and assign those files (and paths) to a variable? For example, bring up a dialogue box that starts you in a base directory, and then allows you to assign files you select to different variables? Currently I have the paths and files hard coded in but this doesnt allow for any variety in the end. The other method i was using was using 24 instances of uigetfile, but this was tedious and time consuming so i swapped back to using hard coded file names.
Reread the uitgetfile documentation, there is an option to allow multiselect files:
[FileName,PathName,FilterIndex] = uigetfile(...,'MultiSelect',selectmode) opens the dialog in multiselect mode. Valid values for selectmode are 'on' and 'off' (the default, which allows single selection only). If 'MultiSelect' is 'on' and you select more than one file in the dialog box, then FileName is a cell array of strings. Each array element contains the name of a selected file. Filenames in the cell array are sorted in the order your platform uses. Because multiple selections are always in the same folder, PathName is always a string identifying a single folder.