MATLAB: making a histogram plot from csv files read and put into cells?
up vote
0
down vote
favorite
Unfortunately I am not too tech proficient and only have a basic MATLAB/programming background...
I have several csv data files in a folder, and would like to make a histogram plot of all of them simultaneously in order to compare them. I am not sure how to go about doing this. Some digging online gave a script:
d=dir('*.csv'); % return the list of csv files
for i=1:length(d)
m{i}=csvread(d(i).name); % put into cell array
end
The problem is I cannot now simply write histogram(m(i)) command, because m(i) is a cell type not a csv file type (I'm not sure I'm using this terminology correctly, but MATLAB definitely isn't accepting the former).
I am not quite sure how to proceed. In fact, I am not sure what exactly is the nature of the elements m(i) and what I can/cannot do with them. The histogram command wants a matrix input, so presumably I would need a 'vector of matrices' and a command which plots each of the vector elements (i.e. matrices) on a separate plot. I would have about 14 altogether, which is quite a lot and would take a long time to load, but I am not sure how to proceed more efficiently.
Generalizing the question:
I will later be writing a script to reduce the noise and smooth out the data in the csv file, and binarise it (the csv files are for noisy images with vague shapes, and I want to distinguish these shapes by setting a cut off for the pixel intensity/value in the csv matrix, such as to create a binary image showing these shapes). Ideally, I would like to apply this to all of the images in my folder at once so I can shift out which images are best for analysis. So my question is, how can I run a script with all of the csv files in my folder so that I can compare them all at once? I presume whatever technique I use for the histogram plots can apply to this too, but I am not sure.
It should probably be better to write a script which:
-makes a histogram plot and/or runs the binarising script for each csv file in the folder
-and puts all of the images into a new, designated folder, so I can sift through these.
I would greatly appreciate pointers on how to do this. As I mentioned, I am quite new to programming and am getting overwhelmed when looking at suggestions, seeing various different commands used to apparently achieve the same thing- reading several files at once.
matlab file csv save
add a comment |
up vote
0
down vote
favorite
Unfortunately I am not too tech proficient and only have a basic MATLAB/programming background...
I have several csv data files in a folder, and would like to make a histogram plot of all of them simultaneously in order to compare them. I am not sure how to go about doing this. Some digging online gave a script:
d=dir('*.csv'); % return the list of csv files
for i=1:length(d)
m{i}=csvread(d(i).name); % put into cell array
end
The problem is I cannot now simply write histogram(m(i)) command, because m(i) is a cell type not a csv file type (I'm not sure I'm using this terminology correctly, but MATLAB definitely isn't accepting the former).
I am not quite sure how to proceed. In fact, I am not sure what exactly is the nature of the elements m(i) and what I can/cannot do with them. The histogram command wants a matrix input, so presumably I would need a 'vector of matrices' and a command which plots each of the vector elements (i.e. matrices) on a separate plot. I would have about 14 altogether, which is quite a lot and would take a long time to load, but I am not sure how to proceed more efficiently.
Generalizing the question:
I will later be writing a script to reduce the noise and smooth out the data in the csv file, and binarise it (the csv files are for noisy images with vague shapes, and I want to distinguish these shapes by setting a cut off for the pixel intensity/value in the csv matrix, such as to create a binary image showing these shapes). Ideally, I would like to apply this to all of the images in my folder at once so I can shift out which images are best for analysis. So my question is, how can I run a script with all of the csv files in my folder so that I can compare them all at once? I presume whatever technique I use for the histogram plots can apply to this too, but I am not sure.
It should probably be better to write a script which:
-makes a histogram plot and/or runs the binarising script for each csv file in the folder
-and puts all of the images into a new, designated folder, so I can sift through these.
I would greatly appreciate pointers on how to do this. As I mentioned, I am quite new to programming and am getting overwhelmed when looking at suggestions, seeing various different commands used to apparently achieve the same thing- reading several files at once.
matlab file csv save
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Unfortunately I am not too tech proficient and only have a basic MATLAB/programming background...
I have several csv data files in a folder, and would like to make a histogram plot of all of them simultaneously in order to compare them. I am not sure how to go about doing this. Some digging online gave a script:
d=dir('*.csv'); % return the list of csv files
for i=1:length(d)
m{i}=csvread(d(i).name); % put into cell array
end
The problem is I cannot now simply write histogram(m(i)) command, because m(i) is a cell type not a csv file type (I'm not sure I'm using this terminology correctly, but MATLAB definitely isn't accepting the former).
I am not quite sure how to proceed. In fact, I am not sure what exactly is the nature of the elements m(i) and what I can/cannot do with them. The histogram command wants a matrix input, so presumably I would need a 'vector of matrices' and a command which plots each of the vector elements (i.e. matrices) on a separate plot. I would have about 14 altogether, which is quite a lot and would take a long time to load, but I am not sure how to proceed more efficiently.
Generalizing the question:
I will later be writing a script to reduce the noise and smooth out the data in the csv file, and binarise it (the csv files are for noisy images with vague shapes, and I want to distinguish these shapes by setting a cut off for the pixel intensity/value in the csv matrix, such as to create a binary image showing these shapes). Ideally, I would like to apply this to all of the images in my folder at once so I can shift out which images are best for analysis. So my question is, how can I run a script with all of the csv files in my folder so that I can compare them all at once? I presume whatever technique I use for the histogram plots can apply to this too, but I am not sure.
It should probably be better to write a script which:
-makes a histogram plot and/or runs the binarising script for each csv file in the folder
-and puts all of the images into a new, designated folder, so I can sift through these.
I would greatly appreciate pointers on how to do this. As I mentioned, I am quite new to programming and am getting overwhelmed when looking at suggestions, seeing various different commands used to apparently achieve the same thing- reading several files at once.
matlab file csv save
Unfortunately I am not too tech proficient and only have a basic MATLAB/programming background...
I have several csv data files in a folder, and would like to make a histogram plot of all of them simultaneously in order to compare them. I am not sure how to go about doing this. Some digging online gave a script:
d=dir('*.csv'); % return the list of csv files
for i=1:length(d)
m{i}=csvread(d(i).name); % put into cell array
end
The problem is I cannot now simply write histogram(m(i)) command, because m(i) is a cell type not a csv file type (I'm not sure I'm using this terminology correctly, but MATLAB definitely isn't accepting the former).
I am not quite sure how to proceed. In fact, I am not sure what exactly is the nature of the elements m(i) and what I can/cannot do with them. The histogram command wants a matrix input, so presumably I would need a 'vector of matrices' and a command which plots each of the vector elements (i.e. matrices) on a separate plot. I would have about 14 altogether, which is quite a lot and would take a long time to load, but I am not sure how to proceed more efficiently.
Generalizing the question:
I will later be writing a script to reduce the noise and smooth out the data in the csv file, and binarise it (the csv files are for noisy images with vague shapes, and I want to distinguish these shapes by setting a cut off for the pixel intensity/value in the csv matrix, such as to create a binary image showing these shapes). Ideally, I would like to apply this to all of the images in my folder at once so I can shift out which images are best for analysis. So my question is, how can I run a script with all of the csv files in my folder so that I can compare them all at once? I presume whatever technique I use for the histogram plots can apply to this too, but I am not sure.
It should probably be better to write a script which:
-makes a histogram plot and/or runs the binarising script for each csv file in the folder
-and puts all of the images into a new, designated folder, so I can sift through these.
I would greatly appreciate pointers on how to do this. As I mentioned, I am quite new to programming and am getting overwhelmed when looking at suggestions, seeing various different commands used to apparently achieve the same thing- reading several files at once.
matlab file csv save
matlab file csv save
asked Nov 9 at 12:28
21joanna12
1175
1175
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
The function csvread returns natively a matrix. I am not sure but it is possible that if some elements inside the csv file are not numbers, Matlab automatically makes a cell array out of the output. Since I don't know the structure of your csv-files I will recommend you trying out some similar functions(readtable, xlsread):
M = readtable(d(i).name) % Reads table like data, most recommended
M = xlsread(d(i).name) % Excel like structures, but works also on similar data
Try them out and let me know if it worked. If not please upload a file sample.
Hi Pablo, thank you for our reply. I figured that it is unecessary to csvread into a cell array, although I may be very wrong about this. I'm not quite sure about the utility of creating the cell array to be honest. All I would like to work with is the csv files, so surely it won't hurt to just keep the structure d, and for each d(i) run M= csvread(d(i).name), apply my code to M and save what I need to another file, and then do the same for the next i? I am getting rather confused between the data types, such as in the code in my original question, the elements of m are referred to as 'cells'
– 21joanna12
Nov 9 at 13:31
but they are csvread files from my directory? How do I go from a 'cell' to the matrix of values in the csv, which is what I need to apply subsequent code to?
– 21joanna12
Nov 9 at 13:32
My recommendation is looking at the matrixMafter reading it and store it in a matrix (which is 3D then) and then go on. I wouldn't use cells if it is not crucial. They are not as efficient as matrices and a bit confusing with the curly and normal brackets.
– Pablo Jeken
Nov 9 at 13:34
Please upload a screenshot of a small csv file you have and how it is read by csvread @21joanna12
– Pablo Jeken
Nov 9 at 13:35
add a comment |
up vote
0
down vote
The function csvread(filename)
always return the matrix M that is numerical matrix and will never give the cell as return.
If you have textual data inside the .csv file, it will give you an error for not having the numerical data only. The only reason I can see for using the cell array when reading the files is if the dimensions of individual matrices read from each file are different, for example first .csv file contains data organised as 3xA, and second .csv file contains data organised as 2xB, so you can place them all into a single structure.
However, it is still possible to use histogram on cell array, by extracting the element as an array instead of extracting it as cell element.
If M is a cell matrix, there are two options for extracting the data:
M(i) and M{i}. M(i) will give you the cell element, and cannot be used for histogram, however M{i} returns element in its initial form which is numerical matrix.
TL;DR use histogram(M{i}) instead of histogram(M(i)).
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
The function csvread returns natively a matrix. I am not sure but it is possible that if some elements inside the csv file are not numbers, Matlab automatically makes a cell array out of the output. Since I don't know the structure of your csv-files I will recommend you trying out some similar functions(readtable, xlsread):
M = readtable(d(i).name) % Reads table like data, most recommended
M = xlsread(d(i).name) % Excel like structures, but works also on similar data
Try them out and let me know if it worked. If not please upload a file sample.
Hi Pablo, thank you for our reply. I figured that it is unecessary to csvread into a cell array, although I may be very wrong about this. I'm not quite sure about the utility of creating the cell array to be honest. All I would like to work with is the csv files, so surely it won't hurt to just keep the structure d, and for each d(i) run M= csvread(d(i).name), apply my code to M and save what I need to another file, and then do the same for the next i? I am getting rather confused between the data types, such as in the code in my original question, the elements of m are referred to as 'cells'
– 21joanna12
Nov 9 at 13:31
but they are csvread files from my directory? How do I go from a 'cell' to the matrix of values in the csv, which is what I need to apply subsequent code to?
– 21joanna12
Nov 9 at 13:32
My recommendation is looking at the matrixMafter reading it and store it in a matrix (which is 3D then) and then go on. I wouldn't use cells if it is not crucial. They are not as efficient as matrices and a bit confusing with the curly and normal brackets.
– Pablo Jeken
Nov 9 at 13:34
Please upload a screenshot of a small csv file you have and how it is read by csvread @21joanna12
– Pablo Jeken
Nov 9 at 13:35
add a comment |
up vote
0
down vote
The function csvread returns natively a matrix. I am not sure but it is possible that if some elements inside the csv file are not numbers, Matlab automatically makes a cell array out of the output. Since I don't know the structure of your csv-files I will recommend you trying out some similar functions(readtable, xlsread):
M = readtable(d(i).name) % Reads table like data, most recommended
M = xlsread(d(i).name) % Excel like structures, but works also on similar data
Try them out and let me know if it worked. If not please upload a file sample.
Hi Pablo, thank you for our reply. I figured that it is unecessary to csvread into a cell array, although I may be very wrong about this. I'm not quite sure about the utility of creating the cell array to be honest. All I would like to work with is the csv files, so surely it won't hurt to just keep the structure d, and for each d(i) run M= csvread(d(i).name), apply my code to M and save what I need to another file, and then do the same for the next i? I am getting rather confused between the data types, such as in the code in my original question, the elements of m are referred to as 'cells'
– 21joanna12
Nov 9 at 13:31
but they are csvread files from my directory? How do I go from a 'cell' to the matrix of values in the csv, which is what I need to apply subsequent code to?
– 21joanna12
Nov 9 at 13:32
My recommendation is looking at the matrixMafter reading it and store it in a matrix (which is 3D then) and then go on. I wouldn't use cells if it is not crucial. They are not as efficient as matrices and a bit confusing with the curly and normal brackets.
– Pablo Jeken
Nov 9 at 13:34
Please upload a screenshot of a small csv file you have and how it is read by csvread @21joanna12
– Pablo Jeken
Nov 9 at 13:35
add a comment |
up vote
0
down vote
up vote
0
down vote
The function csvread returns natively a matrix. I am not sure but it is possible that if some elements inside the csv file are not numbers, Matlab automatically makes a cell array out of the output. Since I don't know the structure of your csv-files I will recommend you trying out some similar functions(readtable, xlsread):
M = readtable(d(i).name) % Reads table like data, most recommended
M = xlsread(d(i).name) % Excel like structures, but works also on similar data
Try them out and let me know if it worked. If not please upload a file sample.
The function csvread returns natively a matrix. I am not sure but it is possible that if some elements inside the csv file are not numbers, Matlab automatically makes a cell array out of the output. Since I don't know the structure of your csv-files I will recommend you trying out some similar functions(readtable, xlsread):
M = readtable(d(i).name) % Reads table like data, most recommended
M = xlsread(d(i).name) % Excel like structures, but works also on similar data
Try them out and let me know if it worked. If not please upload a file sample.
answered Nov 9 at 12:59
Pablo Jeken
449115
449115
Hi Pablo, thank you for our reply. I figured that it is unecessary to csvread into a cell array, although I may be very wrong about this. I'm not quite sure about the utility of creating the cell array to be honest. All I would like to work with is the csv files, so surely it won't hurt to just keep the structure d, and for each d(i) run M= csvread(d(i).name), apply my code to M and save what I need to another file, and then do the same for the next i? I am getting rather confused between the data types, such as in the code in my original question, the elements of m are referred to as 'cells'
– 21joanna12
Nov 9 at 13:31
but they are csvread files from my directory? How do I go from a 'cell' to the matrix of values in the csv, which is what I need to apply subsequent code to?
– 21joanna12
Nov 9 at 13:32
My recommendation is looking at the matrixMafter reading it and store it in a matrix (which is 3D then) and then go on. I wouldn't use cells if it is not crucial. They are not as efficient as matrices and a bit confusing with the curly and normal brackets.
– Pablo Jeken
Nov 9 at 13:34
Please upload a screenshot of a small csv file you have and how it is read by csvread @21joanna12
– Pablo Jeken
Nov 9 at 13:35
add a comment |
Hi Pablo, thank you for our reply. I figured that it is unecessary to csvread into a cell array, although I may be very wrong about this. I'm not quite sure about the utility of creating the cell array to be honest. All I would like to work with is the csv files, so surely it won't hurt to just keep the structure d, and for each d(i) run M= csvread(d(i).name), apply my code to M and save what I need to another file, and then do the same for the next i? I am getting rather confused between the data types, such as in the code in my original question, the elements of m are referred to as 'cells'
– 21joanna12
Nov 9 at 13:31
but they are csvread files from my directory? How do I go from a 'cell' to the matrix of values in the csv, which is what I need to apply subsequent code to?
– 21joanna12
Nov 9 at 13:32
My recommendation is looking at the matrixMafter reading it and store it in a matrix (which is 3D then) and then go on. I wouldn't use cells if it is not crucial. They are not as efficient as matrices and a bit confusing with the curly and normal brackets.
– Pablo Jeken
Nov 9 at 13:34
Please upload a screenshot of a small csv file you have and how it is read by csvread @21joanna12
– Pablo Jeken
Nov 9 at 13:35
Hi Pablo, thank you for our reply. I figured that it is unecessary to csvread into a cell array, although I may be very wrong about this. I'm not quite sure about the utility of creating the cell array to be honest. All I would like to work with is the csv files, so surely it won't hurt to just keep the structure d, and for each d(i) run M= csvread(d(i).name), apply my code to M and save what I need to another file, and then do the same for the next i? I am getting rather confused between the data types, such as in the code in my original question, the elements of m are referred to as 'cells'
– 21joanna12
Nov 9 at 13:31
Hi Pablo, thank you for our reply. I figured that it is unecessary to csvread into a cell array, although I may be very wrong about this. I'm not quite sure about the utility of creating the cell array to be honest. All I would like to work with is the csv files, so surely it won't hurt to just keep the structure d, and for each d(i) run M= csvread(d(i).name), apply my code to M and save what I need to another file, and then do the same for the next i? I am getting rather confused between the data types, such as in the code in my original question, the elements of m are referred to as 'cells'
– 21joanna12
Nov 9 at 13:31
but they are csvread files from my directory? How do I go from a 'cell' to the matrix of values in the csv, which is what I need to apply subsequent code to?
– 21joanna12
Nov 9 at 13:32
but they are csvread files from my directory? How do I go from a 'cell' to the matrix of values in the csv, which is what I need to apply subsequent code to?
– 21joanna12
Nov 9 at 13:32
My recommendation is looking at the matrix
M after reading it and store it in a matrix (which is 3D then) and then go on. I wouldn't use cells if it is not crucial. They are not as efficient as matrices and a bit confusing with the curly and normal brackets.– Pablo Jeken
Nov 9 at 13:34
My recommendation is looking at the matrix
M after reading it and store it in a matrix (which is 3D then) and then go on. I wouldn't use cells if it is not crucial. They are not as efficient as matrices and a bit confusing with the curly and normal brackets.– Pablo Jeken
Nov 9 at 13:34
Please upload a screenshot of a small csv file you have and how it is read by csvread @21joanna12
– Pablo Jeken
Nov 9 at 13:35
Please upload a screenshot of a small csv file you have and how it is read by csvread @21joanna12
– Pablo Jeken
Nov 9 at 13:35
add a comment |
up vote
0
down vote
The function csvread(filename)
always return the matrix M that is numerical matrix and will never give the cell as return.
If you have textual data inside the .csv file, it will give you an error for not having the numerical data only. The only reason I can see for using the cell array when reading the files is if the dimensions of individual matrices read from each file are different, for example first .csv file contains data organised as 3xA, and second .csv file contains data organised as 2xB, so you can place them all into a single structure.
However, it is still possible to use histogram on cell array, by extracting the element as an array instead of extracting it as cell element.
If M is a cell matrix, there are two options for extracting the data:
M(i) and M{i}. M(i) will give you the cell element, and cannot be used for histogram, however M{i} returns element in its initial form which is numerical matrix.
TL;DR use histogram(M{i}) instead of histogram(M(i)).
add a comment |
up vote
0
down vote
The function csvread(filename)
always return the matrix M that is numerical matrix and will never give the cell as return.
If you have textual data inside the .csv file, it will give you an error for not having the numerical data only. The only reason I can see for using the cell array when reading the files is if the dimensions of individual matrices read from each file are different, for example first .csv file contains data organised as 3xA, and second .csv file contains data organised as 2xB, so you can place them all into a single structure.
However, it is still possible to use histogram on cell array, by extracting the element as an array instead of extracting it as cell element.
If M is a cell matrix, there are two options for extracting the data:
M(i) and M{i}. M(i) will give you the cell element, and cannot be used for histogram, however M{i} returns element in its initial form which is numerical matrix.
TL;DR use histogram(M{i}) instead of histogram(M(i)).
add a comment |
up vote
0
down vote
up vote
0
down vote
The function csvread(filename)
always return the matrix M that is numerical matrix and will never give the cell as return.
If you have textual data inside the .csv file, it will give you an error for not having the numerical data only. The only reason I can see for using the cell array when reading the files is if the dimensions of individual matrices read from each file are different, for example first .csv file contains data organised as 3xA, and second .csv file contains data organised as 2xB, so you can place them all into a single structure.
However, it is still possible to use histogram on cell array, by extracting the element as an array instead of extracting it as cell element.
If M is a cell matrix, there are two options for extracting the data:
M(i) and M{i}. M(i) will give you the cell element, and cannot be used for histogram, however M{i} returns element in its initial form which is numerical matrix.
TL;DR use histogram(M{i}) instead of histogram(M(i)).
The function csvread(filename)
always return the matrix M that is numerical matrix and will never give the cell as return.
If you have textual data inside the .csv file, it will give you an error for not having the numerical data only. The only reason I can see for using the cell array when reading the files is if the dimensions of individual matrices read from each file are different, for example first .csv file contains data organised as 3xA, and second .csv file contains data organised as 2xB, so you can place them all into a single structure.
However, it is still possible to use histogram on cell array, by extracting the element as an array instead of extracting it as cell element.
If M is a cell matrix, there are two options for extracting the data:
M(i) and M{i}. M(i) will give you the cell element, and cannot be used for histogram, however M{i} returns element in its initial form which is numerical matrix.
TL;DR use histogram(M{i}) instead of histogram(M(i)).
answered Nov 9 at 14:21
hazeiio
1796
1796
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225725%2fmatlab-making-a-histogram-plot-from-csv-files-read-and-put-into-cells%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown