loading multiple .mat files in MATLAB - matlab

I have 110 files named time1.mat, time2.mat ..., time110.mat. I want to load these matrices into the MATLAB workspace.
I have always used load -'ASCII' matrix.mat to load an ASCII matrix file in the current folder.
So I tried doing
for i=1:10
filename=strcat('time',int2str(i),'.mat');
load -'ASCII' filename
end
But I am getting a MATLAB error as
??? Error using ==> load
Unable to read file filename: No such file or directory.
�
Of course the string filename seems to be evaluated correctly by MATLAB as time1.mat. in the first iteration where it crashes at the load line.
Any suggestions how I should do this?

Use load(filename, '-ascii')

Related

Matlab error while creating csv file from image set using imread and csvwrite

I want to convert a set of images into a csv file. I was working with Matlab and I need each row corresponding to one image. I tried to do it with the following code
I=imread(c{n});
csvwrite('C:\Users\HP\Desktop\test.csv',I(:).','-append'); % c{n} contains the name of image files to be taken
but I am getting the following error
Error using dlmwrite (line 112)
Invalid attribute tag: ,.
Error in csvwrite (line 42)
dlmwrite(filename, m, ',', r, c);
Error in Untitled (line 7)
csvwrite('C:\Users\HP\Desktop\test.csv',I(:).','-append');
but if I try to do it without the '-append' there is no error.
How to change the code such that it takes all the images at once and produces a csv file with the single execution of the code.
csvwrite is meant for writing comma separated values, so adding that ',' is wrong. Then you have put a dot('.') after I(:), which is also wrong. I think you should better use dlmwrite if you want to append the files. It would go like dlmwrite('C:\Users\HP\Desktop\test.csv',I(:)','-append') (since you want each image as one row, you need to transpose the array).
For using this on all images, start by reading all the images into a cell array & then you use cellfun(#(x) dlmwrite('C:\Users\HP\Desktop\test.csv',x(:)','-append'),a). Or for a much simpler version just run the lines in your code inside a for loop.

MATLAB won't open a file created by Octave

I generated and saved large number of data files using Octave, and now I need to open them in MATLAB as part of analyzing them. MATLAB spits out this error.
Error using load
Unable to read MAT-file
[file path]/PhPar_40.mat: not a binary MAT-file.
Try LOAD -ASCII to read as text.
Trying its suggestion of load -ASCII then gives this error.
Error using load
Number of columns on line 2 of ASCII file
[filepath]/PhPar_40.mat must be the same as previous lines.
I (now) understand that Octave is capable of saving in a MATLAB readable format, but re-creating these data files would take an inordinate amount of time and really isn't an option. Is there a way to get MATLAB to read these files?
MATLAB can't open these files because these are not saved by octave properly. Try saving them in octave by following command:
save -mat7-binary '[filepath]/PhPar_40.mat' 'm'
If you have large number of files, you can place all files in folder and then run an iterator to read all load and save in correct format automatically. This iterator will look like:
file = dir('[filepath_read]/*.mat');
index = 1;
while (index==length(file)+1)
m = load('file(index).name')
save -mat7-binary strcat("[filepath_write]/", file(index).name, ".mat") 'm';
index = index+1;
pause(1);
endwhile
Once you have all the files converted to right format, load them in MATLAB. I hope it will solve your problem

Error when load file .dat in matlab

I have problem when I try to load file .dat in Matlab.
My file .dat about speech data:
% Read data file "orig.dat" with sampling rate of 8 kHz
% create an example sound
fs=8000;
t=0:1/fs:3;
x = 1*sin(2*pi*4*t)+0.25* sin(2*pi*560*t);
% play it back
%sound(x, 8000);
wavwrite(x,fs,16,'test56.wav');
y=wavread('test56.wav')
save y.dat y
load y.dat
There is a error:
??? Error using ==> load Number of columns on line 1 of ASCII file C:\Program Files\MATLAB\R2010b\bin\doan\y.dat
must be the same as previous lines.
Error in ==> twosubband at 8 load y.dat; % Load speech data
I don't understand.
Help me fix it.
load expects a file in its own data format. Try saving y as y.mat instead of y.dat.
That is, replace y.dat in both the line where you save and the line where you load by y.mat.
That should do the trick.

Saving data to file in different path in Matlab

I am trying to save some data from current workspace in Matlab to a different folder. I tried using
save('c:\stp\vtp\train.txt','data','-ASCII');
where data is a double matrix. It gives me error message
??? Error using ==> save
Unable to write file c:\stp\vtp\train.txt: No such file
or directory.
I tried using fullfile syntax, even then it is the same case. My current working folder is in different path.
You probably need to run mkdir first. For example:
%Some data to save
x = 1;
%Try to save it in a deep, non-existent directory
save(fullfile(tempdir,'sub1','sub2','sub3','sub4','data.mat'),'x');
% This will probably recreate your error
%To fix, first create the directory
mkdir(fullfile(tempdir,'sub1','sub2','sub3','sub4'))
%Now save works
save(fullfile(tempdir,'sub1','sub2','sub3','sub4','data.mat'),'x') %No error

How do I load an image file in Matlab?

I have to use Matlab to read a picture and make a joint histogram and I'm new to Matlab. When I try to read the Image by using imread function it does not work.
h= imread('a.tif');
??? Error using ==> imread at 363
File "a.tif" does not exist.
Can anyone help me figure out this problem?
One easy way to make sure you have the right path and file name is uigetfile.
To determine your path and filename, use
[filename,path]=uigetfile();
Then modify the code you have written to include the path as well. (If you're new to MATLAB, the syntax for combining two string arrays is [str1, str2])
Perhaps a.tif doesn't exist, or is located in the wrong directory ?
Since you didn't specify a path to the file then it needs to be in MATLAB's working directory (probably the same directory as where your .m file resides). Alternatively you can just specify a full (absolute) path to the .tif file.
Doesn't exist mean this image isn't supported by Matlab library
So, you should browse an image from your files but you must write URL FOR the Image like this :
a= (' D:\images\Angry Birds\bird.jpg');
imshow(a)
You need to load the picture first go to workspace then import data and select your file