How to read this line in MATLAB? - matlab

I came across the following lines in MATLAB:
m = dir(fullfile(dataset,'*.png'));
m = {m(~[m.isdir]).name};
I understand that the first line is trying to obtain the .png files from a directory. But, what is the second line trying to perform? isdir seems to determine that an input is a directory. That's what I new for that part. But, what is the line trying to perform?
Thanks.

The second line is getting all files that are not a directory and then getting the respective names and storing them into a cell array
m.isdir indicates if it is a folder or not
returns 1 if it is, 0 if not.
~[m.isdir] will indicate which of the values returned from isdir was a 0.
m(~[m.isdir]) grabs all objects in m determined by the logical indexing done above
m(~[m.isdir]).name gets the names of all of them
{m(~[m.isdir]).name} stores them all in cell array
Hopefully this step by step walkthrough helps.
While I am not sure why the second line is necessary because the fullfile(dataset,'*.png') should only return paths that end in .png, which will not be a folder, I guess it is good to check.

Related

How do i open a number of xlsx files when I only know part of the filename in Matlab?

I am using Matlab 2013b. I have a list of excel files in a directory and I want to open chosen ones within a loop to read out the data.
I only know the start of each of the file names however, not the ending. I have a vector of numbers that provide the identifying portion of the file's name (filenumber), and I want to loop through the excel files one by one, opening them and extracting the data, then closing them.
There are 500 files, each of the format: img_****ff*******.xlsx, where the first set of asterisks is my filenumber, while the second set of asterisks is unknown.
So far, I have tried listing what is in the directory using:
list=dir('E:\processed\Img*');
filenames={list.name}
This provides with with the full file names.
I tried then within a loop to create the part of the filename I know exists:
x = sprintf('Img_%d_FF_',img(1,1));
I then thought I could use 'Find' to look for my my partial filename/string in the 'filenames' structure above. I don't think I have the code correct for this datatype though:
index = find(strcmp({list.name}, x)==1)
You're pretty close, but the issue is that strcmp compares the whole string and since you only have the beginning part, it is not going to match. I would use strncmp to compare only the first n characters of the string. We can determine what n is based upon the length of your string x.
matches = strncmp({list.name}, x, numel(x));
thisfile = list(matches).name;

How to save a mat file in another directory in matlab

I want to save a matrix (e.g. "PTX_Data_Raw.mat") in another folder (e.g. Temp folder). I have written below code:
mkdir('D:\Projects\ProgrammingPart\Method2_FinalApproved\8-congruent','Temp');
filename=('D:\Projects\ProgrammingPart\Method2_FinalApproved\8-congruent\Temp');
save(filename,'PTX_Data_Raw.mat');
but it didn't work. Does anybody can help me for solving this problem?
THX
Going with your comments, you are using save wrong. The first parameter is the filename you want to call the MAT file and second parameter and onwards are the variables you want to save.
Therefore, you need to make sure filename contains the entire filename, including the path followed by the actual name of the MAT file you want. After, the second parameter is PTX_Data - the name of the matrix you want to save.
mkdir('D:\Projects\ProgrammingPart\Method2_FinalApproved\8-congruent','Temp');
%// Change
filename=('D:\Projects\ProgrammingPart\Method2_FinalApproved\8-congruent\Temp\PTX_Data_Raw.mat');
save(filename,'PTX_Data'); %// Change

How to create blank .mat file from terminal?

Is there any way to create an empty .mat file from a terminal session? Basically, what I am doing is brain graph analysis. The software I am using, if an entire brain is scrubbed (ie, if the displacement of the brain is greater than a certain threshold) the output file will be left out or will be very small. When analyzing, however, I need to be able to eliminate both subjects from the analysis if the entire brain is scrubbed/too much of the brain is scrubbed. To accomplish this, the easiest way would be to simply check the dimensions of the output file within matlab, and if they are below the arbitrary threshold I decide then both subjects will just be skipped over for analysis. The issue is, I can easily check if a file contains too few remaining frames, however, if the resulting file contains no frames, it will entirely just not exist. As the outputs are all sorted, the only thing I need to do is check consecutive files' dimensions, and if one of the files does not contain enough values, then I can simply skip over it entirely. Simply touching a blank file obviously will not work, since it will not contain any encoding. I hope this is a good explanation for my motivation to do this, and if any of you know of any suggestions, please let me know.
A simple solution would be to create an empty file from Matlab and duplicate the file when needed from the console.
Just open Matlab, set to the destination folder and type this:
clear all
save empty.mat
Then, when needed, copy the file from the console. :)
Saving the contents of an empty struct creates an empty .mat file:
emptyStruct = struct;
save('myFile.mat','-struct','emptyStruct');

concatenating file path to changing folders

I am somewhat new to MATLAB and am trying to set up a changing file path in a loop to go into a series of folders and grab image files from each folder. I'm not sure if the problem is with the concatenated parts of the path itself, or with the wildcard search I am using.
I've used similar changing file paths before that have worked, but this one is giving me a "Index exceeds matrix dimensions" error. I thought it was the '*' element that was problematic (similar concatenated paths have worked for me, but only when I specify a file extension or part of a file name), but I am trying to grab DICOM files that do not have any extension, which might make it difficult.
The line within the for loop is as follows:
inputs{1, crun} = cellstr(spm_select('FPList'[allinput,'T1Rawunzip',filesep,OrderForDicoms3{crun,1}],'*'));
I've tried different ways of specifying this--using spm_select, not using spm_select, using commas instead of filesep or vice versa, but nothing has worked.
Any advice would be very much appreciated.
(for reference:
crun is the counter the moves the loop forward, 'allinput' is a previously-specified path, OrderForDicoms3 is a .mat file with a list of folder names that are being individually concatenated to the path each time the loop runs)
Thanks!
-Victoria
I can tell you the most general approach of grabbing files from a folder. If you specify the input folder through uigetdir, then all the files can be grabbed using dir command:
folder = uigetdir;
files = dir(folder);
for i =1:length(files)
if(~files.isdir())
filename = fullfile(folder, files(i).name);
% ... read in the data %
end
end
You can always do it for multiple levels.

In Matlab, how do I create a CSV file from a subset of the lines in a text file?

I need to open a text file and convert it into a CSV file in Matlab. The first 3 lines of the text file are sentences that need to be omitted. The next 28 lines are numbers that need to make up the first column of the CSV, and then the next 28 lines need to make up the second column.
The text file is called datanal.txt and the output file can be named anything. Any help would be appreciated.
Don't have Matlab now to test, but try this. Your input file should be in Matlab's current directory, or put the full path to the file name.
A = csvread('datanal.txt',3,0);
A = reshape(A,28,2);
csvwrite('output.csv',A)
well you can add #'s in front of the first 3 lines then use load and a reshape. Did you need a fully automated script or is there only one file? If you're familiar with matlab at all there are a bunch of ways to turn that large column vector into a matrix.