how to import data from multiple folders in matlab? [duplicate] - matlab

This question already has answers here:
How to get all files under a specific directory in MATLAB?
(8 answers)
Closed 8 years ago.
I have my data in multiple folders. Let's say I have a folder containing 100 subfolders at the moment that look like this:
/folder/Re0001/vitesse
/folder/Re0002/vitesse
/folder/Re000N/vitesse
/folder/Re000N+1/vitesse
/folder/Re0100/vitesse
I want to import the vitesse file into a cell array. This is the code that i am using at the moment:
numfiles=100;
mydata=cell(1,numfiles);
for i=1:numfiles
mydata{i}=uiimport;
end
It is a working solution.
However, if it involves 100 or more files I have to specify each folders and files manually, which is very troublesome.
By the way I am new to Matlab so can you please incorporate example code with the directory given.

I did something similar few days ago. Have a look at the matlab function ls. If you are using windows system, you are all set. If your are using linux, you may need to split the results. However newer version of matlab have the strsplit function that will do the job, or you will use regular expressions.
In your case,
list = ls('/folder/*/vitesse');
will give you a list of your files.

Related

Is there any concept of defining global variables that can be used by various files in a project? [duplicate]

This question already has an answer here:
How to read a CMake Variable in C++ source code
(1 answer)
Closed 6 years ago.
In my project, I want various C files, Header files, cmake files, rpm spec files, java properties files etc to use a single macro(say version number) (or atleast fetch version from a single file). How can i realise it?
For different source code types it will be tricky to use the same include mechanism. But you can use an autocode to create different output files eg. (systemversion.h, systemversion.java, systemversion.txt) and include them into your cpp/java/rpm files.

How to tell which M file Octave / Matlab is running [duplicate]

This question already has answers here:
Find location of current m-file in MATLAB
(4 answers)
Closed 6 years ago.
How can I tell / export / copy the current M file being run to a directory.
I know about the copy command I'm just not sure how to get the current M file that is being run.
The reason for this is that I try out the same M files with different edits that create various output files. And I would like to keep the M file changes with the exported files it creates together.
Thanks
PS: I'm using octave 3.8.1 which is like matlab
Use mfilename
p = mfilename('fullpath')
The fullpath option returns the complete path, which directly allows you to use it with the copy command.
Daniel's solution answers your immediate question, but perhaps there is a better way to organize your workflow.
Maybe in addition to creating the output files, you can export a "parameters" file alongside the outputs which describe all parameters needed to recreate the experiment. This can be as simple as calling save to create a MAT-file containing necessary variables... Just an idea :)
My suggestion is to write a script in whatever shell you have available to invoke octave with your M-file where you also copy your M-file and output.
https://www.gnu.org/software/octave/doc/interpreter/Command-Line-Options.html
Thanks Daniel
This code may help someone else that needs to do this
currentfile=strcat(mfilename('fullpath'),'.m') %current file being run with path
[pathstr,name,ext] = fileparts(currentfile) % split into parts
currentfilecpyto=strcat('/tmp/A_',name,ext) %where to copy file to
copyfile(currentfile, currentfilecpyto) %copy file

Changing Matlab function from calling user listed files to call all files with same extension [duplicate]

This question already has answers here:
process a list of files with a specific extension name in matlab
(4 answers)
Closed 7 years ago.
I am working with this routine below that performs the specific M routine for all listed ascii files that I list as follows:
files={'file1name.asc', 'file2name.asc', 'fileNname.asc'};
ratios=NaN*zeros(1,length(files));
for i=1:length(files)
ratios(i)=specific_m_routine(files{i});
end
How do I simply change this to call all .asc files in the directory, rather than listing each fileNname.asc?
Thanks!
I'm assuming you want to pass a filename to your specific_m_routine function? If so, you need to use dir and files(k).name rather than listing all of your files in a cell array and referring to them with files{k}.
files = dir('*.asc');
ratios = nan(1, numel(files));
for k = 1:numel(files)
ratios(k) = specific_m_routine(files(k).name);
end
Or if you want to be able to use any directory.
folder = fullfile('path', 'to', 'data');
files = dir(fullfile(folder, '*.asc'));
ratios = nan(1, numel(files));
for k = 1:numel(files)
ratios(k) = specific_m_routine(fullfile(folder, files(k).name));
end
This way will not require the files you're trying to load to be in the current directory.
As a side note, I feel like you have asked some variant of this exact question the past few days but haven't taken the time to digest the answers you have gotten. Please go back and review all of them to get a better idea of what you're trying to do.

How to find a 1-digit word using regular expression [duplicate]

This question already has answers here:
List all files in a directory given a regular expression / a set of extensions (Matlab)
(3 answers)
Matlab equivalent of `endsWith`: How to filter list of filenames regarding their extension?
(1 answer)
Closed 7 years ago.
I want to be able to detect a file by looking at its extension:
.m,
.fig,
.mdl,
.slx
but not
.mat
or others.
The final use will be to call DIR with a regular expression that filters out all the unnecessary files.
My first obstacle was to define a word of a single character "m". I couldn't figure out which expression should I use for REGEXP, and the documentation of Matlab was not clear enough.
An example of usage will be helpful, and a reference to a more understandable place will be very appreciated.

How to print matlab struct into a text file? [duplicate]

This question already has answers here:
How to save a structure array to a text file
(3 answers)
Closed 8 years ago.
I am an R user: I don't use Matlab. However, I am working with a genomic file from the Broad Institute called "hg18_with_miR_20080407.mat". You can find it at: http://genepattern.broadinstitute.org/ftp/distribution/genepattern/dev_archive/GISTIC/broad.mit.edu:cancer.software.genepattern.module.analysis/00125/1.1/
I tried to use the R package R.matlab (table <- readMat("~/desktop/hg18_with_miR_20080407.mat")), but it keeps loading in definitively. Hence, since I need that file today, I used a friend's computer with Matlab. I have a struct <1x26835> called rg
Each variable has the following values:
refseq 'NM_003585'
gene 'double C2...'
symb 'DOC2B'
locus_id 8447
...
Is there a way that I can print each entry into a text file that I could easily parse? Is there a better way? I am reading the Matlab documentation but would appreciate if someone could give me a one-liner. If not possible, how could I search each variable for a specific gene entry? I keep getting errors. For example:
find(rg == 'Met')
Error: The expression to the left of the equals sign is not a valid target for an assignment.
Thank you!
I recommend trying YAML files (site).
There are libraries for writing matlab data into it here that I have used and liked and googling shows me that there appear to be libraries for R as well.
YAML is basically a newer version of XML files that is human readable, simpler, and less extensible.