How to import a cell array data 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 6 years ago.
Improve this question
I have a set of data in a text file with the format like below. How shall I import the data into MATLAB? Thanks!
{(38.7948,1319,0.8571,119,0),(39.0693,138,0.0897,21,0),(40.7911,63,0.0409,7,0),(103.4512,19,0.0123,5,0),(-26.0424,223,1.0000,28,0)}
{(35.8689,110,0.5093,14,0),(47.7915,41,0.1898,7,0),(59.7489,53,0.2454,7,0),(71.7298,12,0.0556,3,0)}

It's always helpful if you in your question explain what you have tried so far, what the desired result would be or how you inteend to use it. That way the person answering the question don't have to assume a lot of things.
Here I assume that you would like to import each line as a cell with a set of number arrays within.
To get matlab to evalutate the expression correct the parentheses in the brackets need to be replaced
{(1,2),(3,4)}
Error: Expression or statement is incorrect
{[1,2],[3,4]}
ans =
[1x2 double] [1x2 double]
To read the file you could use fopen and then fgetl to get each line. When the result from fgetl isn't a char, the end of the file (EOF) is reached.
% Open file
f = fopen('...path\to\file.txt','r');
C = {};
while true
% Read each line
fStr = fgetl(f);
if ischar(fStr)
% Replace parentheses and evaluate expresission
C{end+1} = eval(regexprep(fStr,{'(',')'},{'[',']'}));
else
% End of file
break
end
end
fclose(f)
Perhaps you need to include some error checks if the data in your file should be formated incorrect. You could also check out other ways to read data, for exmple fread or fscanf

Related

Changing function variable to another variable 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 3 months ago.
Improve this question
Is there a way to change a function variable to another variable?
for instance, if I have the function:
f = #(s) exp(2-s)
I need to change it to
f = #(t) exp(2-t)
without changing it manually.
With the usual caveats that this is a terrible idea :) let's get a little nuts.
First, we can make the anonymous function f
f = #(s) exp(2-s)
For some sample uses, for sanity
f(1) % 2.71828182845905
f(10) % 0.000335462627902512
Now, save it to a file, using the -v7.3 flag to force an H5 file type
save('tempfile','f','-v7.3')
Matlab can read this file as data. After some exploring, we can see that the function is stored, as an evaluation-ready string, here:
char(h5read('tempfile.mat','/f/function_handle/function'))
% Returns 'sf%0#(s)exp(2-s)'
So, it's easy enough to change, using Matlab's H5 write functions
h5write('tempfile.mat','/f/function_handle/function',uint16('sf%0#(x)exp(2-x)'))
Let's see, did it work?
clear f
load('tempfile.mat')
Now we have the following
f =
function_handle with value:
#(x)exp(2-x)
f(1) % 2.71828182845905
f(10) % 0.000335462627902512
Now, we can see a lot of this data without the file saving business, using the functions function. (So meta)
>> f = #(s) exp(2-s);
>> functions(f)
ans =
struct with fields:
function: '#(s)exp(2-s)'
type: 'anonymous'
file: ''
workspace: {[1×1 struct]}
within_file_path: '__base_function'
This lets you see how Matlab is storing the anonymous function. When debugging, this allows you to see what baggage (stored workspaces and stuff) is associated with the function handle.
However, changes to this structure do not change the actual function handle. I don't know of a way to generate a new function handle from this structure.
Wrap up
[note #1 deleted]
Please, don't ever do this.

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

Save cell arrays in one CSV - Matlab [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 7 years ago.
Improve this question
I have a script which generates a 2 cell array (x and y coordinates). How can I merge them (two columns), save them in one CSV file so I can read them later in Excel?
Below some simplified code which has two solutions that might help you
x = 1:10;
y = 11:20;
x = num2cell(x);
y = num2cell(y);
x2 = cell2mat(x);
y2 = cell2mat(y);
newmat = [x2;y2]';
csvwrite('output.csv',newmat)
fid = fopen ('output2.csv','w');
for i = 1:length(x)
fprintf (fid,'%f, %f\n',x{i},y{i});
end
fclose (fid);
related to what GameOfThrows mentions this only works if x and y have the same length
Note the first solutions converts the cell to an arra which might not always give the result as shown in the example. The second is a more general one with a formatted output...

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!