I have a variable (strings) that contains 192x14 cell array. I want to write this table into a text file. When I use fprintf, it says that "Function is not defined for 'cell' inputs."
For example:
strings = {'foo','bar','baz'};
fprintf('%s ',strings);
produces the error message
Error using fprintf
Function is not defined for 'cell' inputs.
Is there any other function that can do the trick?
To avoid the error try
fprintf('%s\n', cellarray{:});
that will write the elements of callarray one per line
Related
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.
The filename in question is a MAT file that contains elements in the form of "a - bi" where 'i' signifies an imaginary number. The objective is to separate the real, a, and imaginary, b, parts of these elements and put them into two arrays. Afterwards, a text file with the same name as the MAT file will be created to store the data of the newly created arrays.
Code:
function separate(filename)
realArray = real(filename)
imagArray = imag(filename)
fileIDname = strcat(filename, '.txt')
fileID = fopen(fileIDname, 'w')
% more code here - omitted for readability
end
I am trying to run the above code via command window. Here's what I've tried so far:
%attempt 1
separate testFileName
This does not work as the output does not contain the correct data from the MAT file. Instead, realArray and imagArray contains data based on the ascii characters of "testFileName".
e.g. first element of realArray corresponds to the integer value of 't', the second - 'e', third - 's', etc. So the array contains only the number of elements as the number of characters in the file name (12 in this case) instead of what is actually in the MAT file.
%attempt 2
load testFileName
separate(testFileName)
So I tried to load the testFileName MAT variable first. However this throws an error:
Complex values cannot be converted to chars
Error in strcat (line 87)
s(1:pos) = str;
Error in separate (line xx)
fileIDname = strcat(filename, '.txt')
Basically, you cannot concatenate the elements of an array to '.txt' (of course). But I am trying to concatenate the name of the MAT file to '.txt'.
So either I get the wrong output or I manage to successfully separate the elements but cannot save to a text file of the same name after I do so (an important feature to make this function re-usable for multiple MAT files).
Any ideas?
A function to read complex data, modify it and save it in a txt file with the same name would look approximately like:
function dosomestuff(fname)
% load
data=load(fname);
% get your data, you need to knwo the variable names, lets assume its call "datacomplex"
data.datacomplex=data.datacomplex+sqrt(-2); % "modify the data"
% create txt and write it.
fid=fopen([fname,'.txt'],'w');
fprintf(fid, '%f%+fj\n', real(data.datacomplex), imag(data.datacomplex));
fclose(fid);
There are quite few assumptions on the data and format, but can't do more without extra information.
For some reasons, I need to show a message each time save function is executed. All the code of my program is already written. That's why I want to override the saveMATLAB built-in function.
This is the function:
function save(varargin)
disp(['The file has been saved to ' varargin{1}])
builtin('save',varargin{:})
end
However, it doesn't work and MATLAB returns Error using save.
How can I solve this?
I am assuming varargin is cell array of strings, as in the built-in function save.
The problem is that your version of save doesn't "know" the variables of the caller function. You can use evalin function to evaluate save in the context of the caller function.
In order to do so, you should convert varargin to strings. One way to do so is
function save(varargin)
disp(['The file has been saved to ' varargin{1}])
cmd = ['builtin(''save'',' sprintf(repmat('''%s'',',1,nargin),varargin{:}) ];
cmd(end) = ')';
evalin('caller',cmd)
end
I am saving the contents of an array to different files as follows:
for i=1:10
name = [myfilename num2str(i)]
savevar = myvariable(i)
filename = mat2str([name '.dat'])
save(filename, savevar, '-ascii','-double','-append')
end
I have been fiddling around with this for a while and keep getting the following error:
??? Error using ==> save
Argument must contain a string.
Where am I going wrong?
The arguments of the save command must be strings. Specifically, the second argument must be a string that contains the variable name.
The problem in your case is that savevar is the actual value of the variable, and not its name.
I'm not really getting what type of variable you're trying to save. If it's a matrix, you're just better off saving it as a whole to a single file, like so:
save(filename, 'myvariable', '-ascii', '-double', '-append')
and if you have numerous variables, and you want each variable in a different file (which is a bit closer to your example), I suggest you create a cell array of the variable names:
varname = {'A', 'B', 'C', ...} % # Assuming A, B, C, etc. are actual variables
and then use it in the save command inside a loop:
save(filename, varname{i}, '-ascii', '-double', '-append')
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.