How to create a .mat file in Matlab? [closed] - matlab

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have 25 images in a folder and I want to make a .mat file for a road-sign recognition system.
What are the steps for making a .mat file in Matlab?

There may be a better approach for images but here is what I know. If you want to control what goes into the .mat file you can specify what variables in your workspace will be saved using the save command.
% MATLAB R2017a
X = rand(273,273);
Y = rand(273,273);
Z = rand(273,273);
save FileName X Y Z
This creates a file FileName.mat.
You can access the contents using the load command.
clear
load FileName
To save everything in the workspace to a .mat file, use the save command without specifying the variables to save (MATLAB will then save them all).
W = rand(273,273);
save FileName
See the linked documentation for more options and examples.
This requires you to loop through the images in the folder. A direct approach to this is directly loading the images using a loop over [filepath 'image' num2str(j) '.jpg'] with index j where filepath = 'C:\Users\user1\Folder\ImageFolder\'. This uses string concatenation and the num2str command.
If you need to change your current directory within the script,the cd function is useful.
Related Posts:
store multi images in mat file using matlab
how to write to .mat file matlab

Related

Dont understand the function of cmd_data(ii) = cell2mat(textscan(char(data{i}(ALL_STRT(ii):(ALL_STRT(ii)+4))),'%f')); at all [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
IND_STRT = 0;
ALL_STRT = IND_STRT:12:510;
cmd_data = zeros(length(ALL_STRT),1); %example: x=zeros(1,21) gives you a 1 by 21 matrix
for ii = 1:length(ALL_STRT) %declare variable ii to run from the row of length
if ~isempty(data{i})
cmd_data(ii) = cell2mat(textscan(char(data{i}(ALL_STRT(ii):(ALL_STRT(ii)+4))),'%f'));
end
end
I need to read the EPS from EnduroSat, however i have difficulty understanding the line cmd_data(ii) = cell2mat(textscan(char(data{i}(ALL_STRT(ii):(ALL_STRT(ii)+4))),'%f'));
Im required to utilised MatLab to code and this particular line have an error and i don't understand why.
Whenever you see a complicated line like this in MATLAB, try to break it up.
% find some indices. These values have been selected by the programmer/data, can't say why.
a=ALL_STRT(ii):(ALL_STRT(ii)+4)
% obtain that articular section of the data
b=data{i}(a)
% convert it to a char data type (characters)
c=char(b)
% scan text, and treat them as float
d=textscan(c,'%f')
% the output is a cell array, we want a matrix instead. Make the cell array into a matrix.
cmd_data(ii) = cell2mat(d)
You can read particularly what each of these do better in their documentation pages, and you can see it work if you put a break-point in the code, and see what each of this parts output when you call it. Learn how to debug, is a very very powerful tool

Loading (and computing/plotting) multiple .MAT files [duplicate]

This question already has answers here:
How can I load 100 files with similar names and/or string in just one step in MATLAB?
(3 answers)
Closed 6 years ago.
I have 4 .MAT files that I need to run similar functions on, and plot on same graph. Problem is, if I load first file, it only runs on that file. After the "load" function, there are 163 lines of code to repeat. Some answers I have seen require .Mat files with similar naming convention.
File names are:
M1_N_o
M2_S_o
M3_N-b
M4_S_b
Only a little info is given. If you could provide the code it will be more helpful. So I am assuming a lot of stuffs.
I am assuming that all files have the same variables with same dimensions
First rename files
M1_N_o.mat,M2_S_o.mat,M3_N-b.mat,M4_S_b.mat
to
M1.mat,M2.mat,M3.mat,M4.mat
Matlab Code:
figure
hold on
numberOfFiles=4;
for fileIndex =1:numberOfFiles
fileName=strcat('M',num2str(fileIndex),'.mat');
load(fileName);
% your 163 lines of code
% do your plots
end
hold off
If you dont want to rename the files then
figure
hold on
fileNames={'M1_N_o.mat' ;'M2_S_o.mat'; 'M3_N-b.mat'; 'M4_S_b.mat'}
for fileIndex =1:size(fileNames,1)
load(fileNames{fileIndex});
% your 163 lines of code
% do your plots
end
hold off

Matlab Classification load dataset [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to load and use a dataset in order to run some algorithms(Neural Networks) in Matlab. I've downloaded a dataset from the internet which has instances and attributes.
I've saved that dataset as a plain text file, and also with the extension .data or .mat. But I am not able to import and use it in Matlab.
How should I do? I also have to define a training and a test set after.
Thank you in advance.
I have to mention I am new to Matlab and trying to study it as a hobby.
You can just load the data by:
data = load('wine.data');
Then, you can split the data to training and testing very easily.
Here, I put 70% data for training and 30% for testing, but you could choose other fraction. 60-40 or 80-20
data = data(randperm(end), :);
traindata = data(1:floor(0.7*size(data, 1)), :);
testdata = data(floor(0.7*size(data, 1))+1:end, :);
In the end, when you want to run the classifier, remember that in this dataset, the first column is the label and the rest are features.

Differentiate b/w fscanf and load function in matlab [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In my matlab program i am reading the data file using fscanf and writing the the code code to read all the values.That makes me write several steps.
How to use Load() function to overcome this ad make it simple.
So the way load works is to load variables from MATLAB binary/ascii files. In order to create said files you'll have to use the save function e.g.
octave:3> T = "Hello"
T = Hello
octave:4> save "-binary" "testfile" T
octave:5> clear
octave:6> T
error: 'T' undefined near line 1 column 1
octave:6> load "-binary" "testfile" T
octave:7> T
T = Hello
octave:8>
Sorry I used octave for the example but it's the same code either way. So if you know your going to be using the same data just save it in a MATLAB's binary format. It should save your self the time of having to use fscanf on it next time your playing around with the data.

Import text file as a matrix in a matlab script [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi everybody I need to automaticly import certain text files stored in my computer as matrices when I run my script in matlab. How do I do that? Thanks
Although the question shows little effort I am reminded how I started out with no knowledge about input or output whatsoever, it is a quite dense forest of information really.
Basically to read a file you need to:
Open the file
Read the file and assign it to a variable
Close the file
Some functions in MatLab take care of all three steps:
importdata
csvread
dlmread
The functions above are suitable if you have very neat and uniform data. Click the links to read if they are suitable for you. If your data is less uniform, e.g. it contains both numbers and letters, you might want to consider textscan.
Using textscan you must carry out all three steps yourself. First open your file and create a link to your file called a file ID (FID):
FID = fopen('mytextfile.txt')
Next you define a format specifier which describes a single line of data (a row).
formatSpec = '%f %f %f %f %s'
This format specifier represents 4 decimal numbers (floats) followed by a string all seperated by whitespace. For more information on the format specifier see:
http://www.mathworks.nl/help/matlab/ref/textscan.html#inputarg_formatSpec
Now you can read your text file by calling:
C = textscan(FID,formatSpec);
Which stores each column in a cell in C. So the first column is C{1}, the second C{2}, etc.
Finally make sure you close your file by using the file id:
fclose(FID);
Good luck!