MATLAB script that can call .mat files without directory - matlab

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);

Related

Make a copy of the files in a directory but save them as a different file type

Is it possible to list all of the files in a directory of a certain file type, and then save them in the same directory but as a separate file type using MatLab?
In my case, I have 144 files saved as in a .fig format, but I would like to copy them as a .tif format so that I don't have to go and change each file manually.
I know I can list all the files in my directory using the dir function and I guess I could simply run for loop with i=1:length(dir) but I don't know how to isolate the files of a specific file type. I don't see filetype as a field name on the mathworks website for dir.
Thanks for any suggestions.
To list only files of type .fig:
files = dir('*.fig');
You can then loop over the names:
for k = 1:numel(files)
filename = files(k).name;
% do something with filename
end

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).

Extracting specific file from zip in matlab

Currently I have a zipfile containing several thousand .xml files, extracted the folder is 1.5gb in size.
I have a function that matches data with specific files inside this zip file. I then want to read this specific file and extract additional data.
My question:
Is there any way to extract these specific files from the archive without unzipping the entire archive?
The built in unzip.m function can only be used to unzip the entire file so it won't work so I am thinking I have to use the COM interface or some other approach.
Matlab version: R2013a
While searching for solutions I found this:Read the data of CSV file inside Zip File without extracting the contents in Matlab
But I can't get the code in the answer to work for my situation
Edit:
Credit to Hoki and Intelk
zipFilename = 'HMDB.zip';
zipJavaFile = java.io.File(zipFilename);
zipFile=org.apache.tools.zip.ZipFile(zipJavaFile);
entries=zipFile.getEntries;
cnt=1;
while entries.hasMoreElements
tempObj=entries.nextElement;
file{cnt,1}=tempObj.getName.toCharArray';
cnt=cnt+1;
end
ind=regexp(file,'$*.xml$');
ind=find(~cellfun(#isempty,ind));
file=file(ind);
file = cellfun(#(x) fullfile('.',x),file,'UniformOutput',false);
And not forgetting the
zipFile.close

How to read trc files from different directory in matlab

I have a program that reads data from .trc files. But as of now, it can only read the .trc files that are in the current directory i.e the MATLAB folder. If I want it to read a file, I have to copy the file in MATLAB folder. Is there a way I can move to different directories and choose another .trc file? I have tried using dir, uigetdir etc. but nothing seems to work!
It's pretty straightforward to filter results with the dir command with a file extension if you specify the folder to search with a wildcard character in place of the file name. You can then loop over the generated list of file names in the output structure (as shown) or whatever you need to do with those files.
folderName = 'C:\Path\To\Target\Folder\';
fileList = dir(strcat(folderName, '*.trc'));
for k = 1:length(fileList)
fileHere = fullfile(folderName, fileList(k).name);
% Do what you need with the files in here
end

Matlab multiple image loading / processing

I'm trying to process 77 images on a single matlab script. I have to load these images and use several processing functions on each. The image names are not sequential. How can I do that without explicitly writing all?
Thanks in advance.
you can use dir to get a list of all the files and folders in the current folder you are in, for example
s=dir(fullfile(matlabroot, 'toolbox/matlab/audiovideo'))
returns the contents of the matlab/audiovideo folder, where s.name will contain the names of the files in that folder.
Another example:
s=dir('*.mat')
will return to s.name all the file names in the current folder that are of type .mat
Now you can load these files in a loop:
for n=1:numel(s)
load(s(n).name);
.... % do whatever
end