Why do I get a 0x1 cell? Matlab what function - matlab

I'm using the following code to look through all files in a particular directory, and I'm getting some strange results. The point of the program is to do the following: I'm looking through a huge number (~7000+) of .mat files for each day between 6-20-2007 and 9-20-2007. What I'm looking to do is search through each of these folders and look at the .mat files, etc. However, for some reason I'm getting a 0x1 cell that doesn't make sense to me. Maybe someone with a better trained eye can see why?
jDate = strtok( dates(j).name, '.' ); % Or dates(j,1).name
tradeFolder = sprintf( 'TAQ Data\\trades unzipped\\%s.tar\\%s\\', jDate );
tradeFiles = what(tradeFolder);
tradeMat = tradeFiles.mat;
quoteFolder = sprintf( 'TAQ Data\\quotes unzipped\\%s.tar\\%s\\', jDate );
quoteFiles = what(quoteFolder);
quoteMat = quoteFiles.mat;
(I have excluded the beginnings of the file paths since it includes my name). Anyways, how the data is saved is this: I extracted each day's worth of data and saved it to the folders listed above. Inside trades unzipped, for instance, will be a folder 20070620.tar, and inside that folder will be another folder named 20070620, and inside that folder is over 7000 .mat files. So....how come I'm getting a 0x1 cell for the tradeFiles.mat?
If anyone can help I'd greatly appreciate it.

A few comments
Both sprintf lines you have (tradeFolder=... and quoteFolder=...) has two '%s' in the formatted string, while only one argument: jDate. This might cause undefined behavior.
It is better to use fullfile to concatenate paths and file names.
Although using what in this context is correct, you might want to double-check it using dir( fullfile( tradeFolder, '*.mat' ) );
It is best not to use i and j as variables in Matlab.

Related

Error code in importing multiple csv files from certain folder using matlab

I am really a newbie in matlab programming. I have a problem in coding to import multiple csv files into one from certain folder:
This is my code:
%% Importing multiple CSV files
myDir = uigetdir; %gets directory
myFiles = dir(fullfile(myDir,'*.csv')); %gets all csv files in struct
for k = 1:length(myFiles)
data{k} = csvread(myFiles{k});
end
I use the code uigetdir in order to be able to select data from any folder, because I try to make an automation program so it would be flexible to use by others. The code that I run only look for the directory and shows the list, but not for merging the csv files into one and read it in "import data". I want it to be merged and read as one file.
My merged file should look like this with semicolon delimited and consist of 47 csv files merged together (this picture is one of the csv file I have):
my merged file
I have been working for it a whole day but I find always error code. Please help me :(. Thank you very much in advance for your help.
As the error message states, you're attempting to reference myFiles as a cell array when it is not. The output of dir is a structure, which cannot be indexed like a cell array.
You want to do something like the following:
for k = 1:numel(myFiles)
filepath = fullfile(myFiles(k).folder, myFiles(k).name);
data{k} = csvread(filepath);
end

read multiple file from folder

I want to read multiple files from a folder but this code does not work properly:
direction=dir('data');
for i=3:length(direction)
Fold_name=strcat('data\',direction(i).name);
filename = fullfile(Fold_name);
fileid= fopen(filename);
data = fread (fileid)';
end
I modified your algorithm to make it easier
Just use this form :
folder='address\datafolder\' ( provide your folder address where data is located)
then:
filenames=dir([folder,'*.txt']); ( whatever your data format is, you can specify it in case you have other files you do not want to import, in this example, i used .txt format files)
for k = 1 : numel(filenames)
Do your code
end
It should work. It's a much more efficient method, as it can apply to any folder without you worrying about names, number order etc... Unless you want to specify certain files with the same format within the folder. I would recommend you to use a separate folder to put your files in.
In case of getting access to all the files after reading:
direction=dir('data');
for i=3:length(direction)
Fold_name=strcat('data\',direction(i).name);
filename = fullfile(Fold_name);
fileid(i)= fopen(filename);
data{i-2} = fread (fileid(i))';
end

read image permission in Matlab

I'm trying to access images in a matlab interface
my code is as follows:
global im2 im
axes(handles.axes4);
[path1, user_cance]= imgetfile();
if user_cance
msgbox(sprintf('Error'), 'Error', 'Error');
return
end
srcFiles = dir('C:\Users\User\Desktop\images test\yale faces\yalefaces\..');
% yale faces is the database folder
for i = 1 : length(srcFiles)
file_name=dir(strcat('C:\Users\User\Desktop\images test\yale faces\yalefaces'));
im2=imread(strcat('C:\Users\User\Desktop\images test\yale faces\yalefaces',file_name(i).name));
%processing of read image
end
the issue is that when I run the code, it gives the following error:
Can't open file "C:\Users\User\Desktop\images test\yale faces\yalefaces" for
reading;
you may not have read permission.
Does anyone know how to solve this issue?
When you do a directory listing (without any wildcards) you are going to get the current directory '.' and parent directory as well '..'. You can't read these like files because they are directories. You will need to filter out the directories prior to trying to read them with imread.
files = dir('C:\Users\User\Desktop\images test\yale faces\yalefaces');
% Remove directories
files = files(~[files.isdir]);
As a side note, it is very hard to tell what your code is doing, but I'm pretty sure it doesn't do what you hope.
It seems like you want to get all images within the database. If that's so, you'll want to do something like.
folder = 'C:\Users\User\Desktop\images test\yale faces\yalefaces';
% Get a list of all files in this folder
files = dir(folder);
files = files(~[files.isdir]);
for k = 1:numel(files)
% Append the folder with the filename to get the path and load
im2 = imread(fullfile(folder, files(k).name));
end
I highly discourage using strcat to construct file paths particularly because it removes trailing/leading whitespace from each input which can corrupt a filename. fullfile was designed for exactly this so please use that.

Reading all the files in sequence in MATLAB

I am trying to read all the images in the folder in MATLAB using this code
flst=dir(str_Expfold);
But it shows me output like this. which is not the sequence as i want.
Can anyone please tell me how can i read all of them in sequence?
for giving downmark, please explain the reason for that too.
By alphabetical order depth10 comes before depth2. If at all possible, when creating string + num type filenames, use a fixed width numerical part (e.g. depth01, depth02) - this tends to avoid sorting problems.
If you are stuck with the filenames you have, and know the filename pattern, though, you can not bother using dir at all and create your filename list in the correct order in the first place:
for n = 1:50
fname = sprintf('depth%d.png',n);
% code to read and process images goes here
end
From the Matlab forums, the dir command output sorting is not specified, but it seems to be purely alphabetical order (with purely I mean that it does not take into account sorter filenames first). Therefore, you would have to manually sort the names. The following code is taken from this link (you probably want to change the file extension):
list = dir(fullfile(cd, '*.mat'));
name = {list.name};
str = sprintf('%s#', name{:});
num = sscanf(str, 'r_%d.mat#');
[dummy, index] = sort(num);
name = name(index);

Read multiple non-sequential images from multiple folders

I have a folder (new_images) which contains 52 sub-folders (person1 to person52) and each sub-folders contain 50 images which are not sequential (like: person1 1, person1 3, person1 10). I want to these read images from each sub-folders and do a processing, How can I do this?
I would really appreciate your answers
You can load images using the Matlab imread function.
I'm thinking of a quick way to do that:
new_images_rep = pwd;
for i=1:52
eval(srcat('pics = dir(new_images_rep','/person',num2str(i),')');
for k=1:50
a(i,k) = imread(pics(k+2).name,'fmt'); %there is k+2 because the dir function also stores repositories like '.' or '..'.
end
end
You have to be careful with the dir function. You should test it in your new_images repository in order to see what are the files/repositories stored in pics.
eval is a Matlab function that allows you to execute any Matlab expression. The string created by strcatis here (if i=3):
'pics = dir(new_images_rep/person3)'
Be sure that the new_imagesrepository is in your Matlab path, or replace new_images_rep = pwd; by new_images_rep = 'the_actual_full_path'.
'fmt' is the actual format of the images you want to store (like .tif or .jpg or anything else).
Using the code I gave you (and a few modifications), all the files from the first subfolder will be stored in a(1,:). Do not hesitate to read Matlab help on every function used here.