Fail to Convert .mat to .csv file - matlab

I would like to convert matlab.mat file to a .csv file. Here is the loading procedure:
>> FileData = load('matlab.mat')
FileData =
struct with fields:
MIMICtable: [278340×59 table]
Then I tried to save the CSV file but the following error occurs:
>> csvwrite('file.csv',MIMICtable)
Check for missing argument or incorrect argument data type in call to function 'real'.
Error in csvwrite (line 47)
throw(e)
I am not sure the meaning of the error message. Could someone help?

Your data is a table object. Use the function writetable to write it to a CSV file. csvwrite accepts only numeric arrays as input.
Yes, this is a weird error message. On the other hand, csvwrite is no longer recommended, which means it has been superseded by a newer function, and is no longer maintained.

Related

Matlab Readtable Invalid parameter name: Range

I am trying to read column C from an Excel CSV file (file is too large to load entire thing). I am trying the following code:
filename='AS-1704-CT-Data-(Jan4---Jan-7)_1.csv';
T=readtable(filename, 'Delimiter', ',', 'Range', 'C:C')
The error I get says Error in (line 2), Invalid parameter name: Range.
According to the Matlab doc for readtable, Range is a valid parameter. The Name is 'Range' and the Value is 'C:C' (I've also tried 'C2:C8' while troubleshooting).
Am I missing something here?
MATLAB interprets your file as text, and according to documentation
When reading:
Text files, only these parameter names apply: FileType, ReadVariableNames, ReadRowNames, TreatAsEmpty, DatetimeType, Delimiter, HeaderLines, Format, EmptyValue, MultipleDelimsAsOne, CollectOutput, CommentStyle, ExpChars, EndOfLine, DateLocale, and Encoding.
So Range is not a valid parameter name for text files.
You could try saving your file as an excel workbook (.xls) and reading from that.

How can I load a text file in a specific variable using matlab/Octave?

I want to load train.txt in to a variable named train_org.
But, the following is generating an error?
>> train_org = load train.txt;
parse error:
syntax error
>>> train_org = load train.txt;
^
How can I fix that?
N.B. The text file loads perfectly without that variable name.
You are getting a syntax error because you are using the command syntax to call the load function and you can't assign the output to a variable this way.
Command syntax does not allow you to obtain any values that might be returned by the function. Attempting to assign output from the function to a variable using command syntax generates an error. Use function syntax instead.
You need to use the standard function syntax instead.
train_org = load('train.txt')

Index out of bounds after reading a text file

I have the following simple code, and I tried to use one of the indices from the .txt file. The index that I want is at (4,1) while the size of my matrix in the .txt file is (8,4). When I run the code, MATLAB give me the following error;
Attempted to access q(4,1); index out of
bounds because size(q)=[1,601]
Can someone help me understand why I receive the error and how to fix it?
Here is the code:
q = fileread('sv11edit.txt');
toe = q(4,1)
The answer will depend on the format of the file sv11edit.txt. However, fileread returns a string of characters. In this case, it gives you a string that is 601 characters long. You receive an error because you assume that q is 8 by 4, but this is not the case.
Check what is being stored in q before you try anything like the second line of your code. The function load may be a better alternative to fileread.

Using datestr(now) with save

The code is:
filename = sprintf('michael%s.bat',datestr(now));
...
save (filename,vec)
vec is a vector
I'm getting this error:
Error using save
Argument must contain a string.
Error in sumfnc (line 13)
save (filename,vec)
I'm unsure on how filename is not a string.
The problem is not filename, it is vec. With the functional usage of save, you need to do:
save(filename,'vec')
However, since filename will contain a space, you will also need to modify filename. Try:
save(strrep(filename,' ','_'),'vec')
to replace spaces with _.

Issue on writing a function with string as the argument

I need to write a function whose input argument should be file name, and the function will perform certain operation on the opened file. Here is the sample function I wrote,
function readFile = loadOneColumnFile(fileName)
fid1 = fopen(fileName);
readFile = 0;
fclose(fid1);
But when I invoke this function in the command console as follows,
>> testValue = loadOneColumnCSV('/usr1/test.csv');
The Matlab returns the following error message
??? Undefined function or method 'loadOneColumnFile' for input arguments of type 'char'.
Looks like that the definition of function is not correct. How to fix it? Thanks.
MATLAB treats a string as an array of characters (like C++, except the strings are not null-terminated in MATLAB).
Despite the error message, I don't think there is any problem with the string passing. The problem is MATLAB cannot find your function. So:
The file containing the function must have same name as the function (in your case save the function in a file named loadOneColumnFile.m)
The loadOneColumnFile.m must be placed in the working (current) directory so MATLAB could find it.
The name of the function is not consistent in your question. Make sure you have used only one of the loadOneColumnFile or loadOneColumnCSV for naming the function and filename.