MATLAB function to output variable as delimited .txt - matlab

I want to write a lazy mans command to output any variable I choose in the workspace as a comma delimited .txt file. I know you can use dlmwrite('outName.txt',varname); but I want something where I can simply go out(varname) and it does the same thing.
So far I have come up with this:
First function needed toget the varname as a string name
function out = strfromvar(input)
out = inputname(1);
end
Second function to dlmwrite that string name and variable...
function [out] = toigor(input)
stringName = strfromvar(input);
outName = [stringName '.txt'];
dlmwrite(outName,input);
end
The area I am stuck is at the outName area. I cant get it so it turns my chosen variable into a .txt file. It just calls it 'input'. The file works as anticipated but i'd liek the program to fully work. I am sure I am just missing a small bit here.

This is what does the trick for me:
function toigor(input)
stringName = inputname(1);
outName = [stringName '.txt'];
dlmwrite(outName,input);
end
Note that you only need 1 function.

Related

Create a separate log file for each iteration in a for loop

I just want to make a script which runs multiple files and stores their outputs in separate log files.
So I wrote the code by using diary to generate the output, but the diary function is only 1 log file and the output is updating in the same log file for remaining iterations. In my testconfig_1 file, I have at present only print as the content.
Then I tried to use the fopen method, with this I am getting multiple log files but I don't understand how I can put that data into the log file which I have created through fopen after each run.
% with diary method
clear all;
diary on;
instring ='testconfig_';
for x = 1:3
fprintf ('Simulation on testconfig_%d \n' , x);
test = [instring num2str(x)];
run(test);
diary testconfig_(x).log;
end
% without diary method
clear all;
diary on;
instring ='testconfig_';
for x = 1:3
fprintf ('Simulation on testconfig_%d \n', x);
test = [instring num2str(x)];
run(test);
fid = fopen(sprintf('testconfig_%d.log',x),'w');
end
I wanted to get testconfig_1.log, testconfig_2.log, testconfig_3.log and I wanted to print in_testconfig_1, in_testconfig_2, in_testconfig_3 respectively
You are using command syntax to call the diary function.
% Command syntax
diary filename.log
% Equivalent function syntax
diary( 'filename.log' );
Notice that, when using command syntax, all arguments are treated as strings, despite not having any quote marks!
So when you do diary testconfig_(x).log, the equivalent is
diary( 'diary testconfig_(x).log' );
All of your logs have the same filename, because x is never evaluated as the loop variable, it's always just part of a string!
You are trying to create the strings with the loop variable in the name, for this you cannot use command syntax.
Instead, use function syntax like this:
filename = sprintf( 'diary testconfig_%d.log', x );
diary( filename ); % treat the filename variable as a variable, not a string
You don't have to declare the intermediate filename variable, and there are other ways to create the filename string, but I hope this demonstrates the issue.

Read data from text file and passing to function

I got a this program which takes in all it data from a .txt file. It is possible to read the required data from the text file and pass that data to a function to work with? I have tried reading the data first and passing it to the function but then my plot refuses to work.
Right now I am doing it by sending in the name of the text file to the function and then read the data but this means that I am reading the data each time I call the function and I was hoping that I could just read the data once and then pass it on to the function. I think that not reading the data many times would speed up my program considerable.
My code looks like this
main.m
young bein_AB_light.txt %%calling the function with bein_AB_light.txt as parameter.
young.m
function young(filename)
fid = fopen(filename,'r');
C = textscan(fid,'%*f%*f%*f%*f%f');
fclose(fid);
Y=10500*C{1}.^2.29; %
plot(C{1},Y,'.K')
if(strfind(filename,'AB'))
xlabel('BMD[g/cm^3]');
ylabel('Youngstudull');
title('Reiknadur Youngstudull fyrir AB bein')
else
xlabel('BMD[g/cm^3]');
ylabel('Youngstudull');
title('Reiknadur Youngstudull fyrir SCI bein')
end
end
EDIT...
This is what I was trying but it gives me error when it tries to plot. Plot does not accept filename{1} to use as the X coordinites. I have also tried to use cell2mat function to change the input but that did not work.
main.m
fid = fopen(filename,'r');
AB_Bein = textscan(fid,'%*f%*f%*f%*f%f');
fclose(fid);
young AB_bein %%calling the function with AB_Bein as parameter.
young.m
function young(filename)
Y=10500*filename{1}.^2.29; %
plot(filename{1},Y,'.K')
if(strfind(filename,'AB'))
xlabel('BMD[g/cm^3]');
ylabel('Youngstudull');
title('Reiknadur Youngstudull fyrir AB bein')
else
xlabel('BMD[g/cm^3]');
ylabel('Youngstudull');
title('Reiknadur Youngstudull fyrir SCI bein')
end
end
it's possible that your problem is the way you are calling young.
If I create a function
function fileContents= young(filename)
fid = fopen(filename,'r');
C = textscan(fid,'%*f%*f%*f%*f%f');
fclose(fid);
fileContents=C{1};
and then call it using
fileContents= young('textfile.txt');
rather than
young textfile.txt
That brings the data from the file out into the variable named fileContents

Create a blank text file from within a function in Matlab

I wnat to create a function:
function[check]=createFile(filename, matrix)
Where I create a blank text file with the name 'filename'.
Where later in the function inputs from 'matrix' can be put in and stored.
2 question:
1)How do I create just a blank .txt file?
2)I've had some problem with this in somewhat simular functions, but is there an easy way to get rid of the need to write apostrophes in the arguments when calling the function?(i.e: createFile(name,matrix) instead of createFile('name',matrix)
to create the text file just use:
fid = fopen('filename.txt','w')
and there is no way to avoid the apostrophies 'filename.txt' - as matlab would try to call a function filename.txt which it wouldn't find.
for your function you can use
function [check] = createFile(filename, matrix)
% filename contains string!
fid = fopen(filename,'w')`
if exist('fid')
check = true;
else
check = false;
end
... write your matrix to file.
end
2) but is there an easy way to get rid of the need to write apostrophes in the arguments when calling the function?(i.e: createFile(name,matrix) instead of createFile('name',matrix)
Yes: you can type
createFile name matrix
after you have included this in createFile.m:
function createFile(name,matrix)
matrix=evalin('caller',matrix);

Load Multiple .mat Files to Matlab workspace

I'm trying to load several .mat files to the workspace. However, they seem to overwrite each other. Instead, I want them to append. I am aware that I can do something like:
S=load(file1)
R=load(file2)
etc.
and then append the variables manually.
But there's a ton of variables, and making an append statement for each one is extremely undesirable (though possible as a last resort). Is there some way for me to load .mat files to the workspace (by using the load() command without assignment) and have them append?
Its not entirely clear what you mean by "append" but here's a way to get the data loaded into a format that should be easy to deal with:
file_list = {'file1';'file2';...};
for file = file_list'
loaded.(char(file)) = load(file);
end
This makes use of dynamic field references to load the contents of each file in the list into its own field of the loaded structure. You can iterate over the fields and manipulate the data however you'd like from here.
It sounds like you have a situation in which each file contains a matrix variable A and you want to load into memory the concatenation of all these matrices along some dimension. I had a similar need, and wrote the following function to handle it.
function var = loadCat( dim, files, varname )
%LOADCAT Concatenate variables of same name appearing in multiple MAT files
%
% where dim is dimension to concatenate along,
% files is a cell array of file names, and
% varname is a string containing the name of the desired variable
if( isempty( files ) )
var = [];
return;
end
var = load( files{1}, varname );
var = var.(varname);
for f = 2:numel(files),
newvar = load( files{f}, varname );
if( isfield( newvar, varname ) )
var = cat( dim, var, newvar.(varname) );
else
warning( 'loadCat:missingvar', [ 'File ' files{f} ' does not contain variable ' varname ] );
end
end
end
Clark's answer and function actually solved my situation perfectly... I just added the following bit of code to make it a little less tedious. Just add this to the beginning and get rid of the "files" argument:
[files,pathname] = uigetfile('*.mat', 'Select MAT files (use CTRL/COMM or SHIFT)', ...
'MultiSelect', 'on');
Alternatively, it could be even more efficient to just start with this bit:
[pathname] = uigetdir('C:\');
files = dir( fullfile(pathname,'*.mat') ); %# list all *.mat files
files = {files.name}'; %# file names
data = cell(numel(files),1); %# store file contents
for i=1:numel(files)
fname = fullfile(pathname,files{i}); %# full path to file
data{i} = load(fname); %# load file
end
(modified from process a list of files with a specific extension name in matlab).
Thanks,
Jason

How do I retrieve the names of function parameters in matlab?

Aside from parsing the function file, is there a way to get the names of the input and output arguments to a function in matlab?
For example, given the following function file:
divide.m
function [value, remain] = divide(left, right)
value = floor(left / right);
remain = left / right - value;
end
From outside the function, I want to get an array of output arguments, here: ['value', 'remain'], and similarly for the input arguments: ['left', 'right'].
Is there an easy way to do this in matlab? Matlab usually seems to support reflection pretty well.
EDIT Background:
The aim of this is to present the function parameters in a window for the user to enter. I'm writing a kind of signal processing program, and functions to perform operations on these signals are stored in a subfolder. I already have a list and the names of each function from which the user can select, but some functions require additional arguments (e.g. a smooth function might take window size as a parameter).
At the moment, I can add a new function to the subfolder which the program will find, and the user can select it to perform an operation. What I'm missing is for the user to specify the input and output parameters, and here I've hit the hurdle here in that I can't find the names of the functions.
MATLAB offers a way to get information about class metadata (using the meta package), however this is only available for OOP classes not regular functions.
One trick is to write a class definition on the fly, which contain the source of the function you would like to process, and let MATLAB deal with the parsing of the source code (which can be tricky as you'd imagine: function definition line spans multiple lines, comments before the actual definition, etc...)
So the temporary file created in your case would look like:
classdef SomeTempClassName
methods
function [value, remain] = divide(left, right)
%# ...
end
end
end
which can be then passed to meta.class.fromName to parse for metadata...
Here is a quick-and-dirty implementation of this hack:
function [inputNames,outputNames] = getArgNames(functionFile)
%# get some random file name
fname = tempname;
[~,fname] = fileparts(fname);
%# read input function content as string
str = fileread(which(functionFile));
%# build a class containing that function source, and write it to file
fid = fopen([fname '.m'], 'w');
fprintf(fid, 'classdef %s; methods;\n %s\n end; end', fname, str);
fclose(fid);
%# terminating function definition with an end statement is not
%# always required, but now becomes required with classdef
missingEndErrMsg = 'An END might be missing, possibly matching CLASSDEF.';
c = checkcode([fname '.m']); %# run mlint code analyzer on file
if ismember(missingEndErrMsg,{c.message})
% append "end" keyword to class file
str = fileread([fname '.m']);
fid = fopen([fname '.m'], 'w');
fprintf(fid, '%s \n end', str);
fclose(fid);
end
%# refresh path to force MATLAB to detect new class
rehash
%# introspection (deal with cases of nested/sub-function)
m = meta.class.fromName(fname);
idx = find(ismember({m.MethodList.Name},functionFile));
inputNames = m.MethodList(idx).InputNames;
outputNames = m.MethodList(idx).OutputNames;
%# delete temp file when done
delete([fname '.m'])
end
and simply run as:
>> [in,out] = getArgNames('divide')
in =
'left'
'right'
out =
'value'
'remain'
If your problem is limited to the simple case where you want to parse the function declaration line of a primary function in a file (i.e. you won't be dealing with local functions, nested functions, or anonymous functions), then you can extract the input and output argument names as they appear in the file using some standard string operations and regular expressions. The function declaration line has a standard format, but you have to account for a few variations due to:
Varying amounts of white space or blank lines,
The presence of single-line or block comments, and
Having the declaration broken up on more than one line.
(It turns out that accounting for a block comment was the trickiest part...)
I've put together a function get_arg_names that will handle all the above. If you give it a path to the function file, it will return two cell arrays containing your input and output parameter strings (or empty cell arrays if there are none). Note that functions with variable input or output lists will simply list 'varargin' or 'varargout', respectively, for the variable names. Here's the function:
function [inputNames, outputNames] = get_arg_names(filePath)
% Open the file:
fid = fopen(filePath);
% Skip leading comments and empty lines:
defLine = '';
while all(isspace(defLine))
defLine = strip_comments(fgets(fid));
end
% Collect all lines if the definition is on multiple lines:
index = strfind(defLine, '...');
while ~isempty(index)
defLine = [defLine(1:index-1) strip_comments(fgets(fid))];
index = strfind(defLine, '...');
end
% Close the file:
fclose(fid);
% Create the regular expression to match:
matchStr = '\s*function\s+';
if any(defLine == '=')
matchStr = strcat(matchStr, '\[?(?<outArgs>[\w, ]*)\]?\s*=\s*');
end
matchStr = strcat(matchStr, '\w+\s*\(?(?<inArgs>[\w, ]*)\)?');
% Parse the definition line (case insensitive):
argStruct = regexpi(defLine, matchStr, 'names');
% Format the input argument names:
if isfield(argStruct, 'inArgs') && ~isempty(argStruct.inArgs)
inputNames = strtrim(textscan(argStruct.inArgs, '%s', ...
'Delimiter', ','));
else
inputNames = {};
end
% Format the output argument names:
if isfield(argStruct, 'outArgs') && ~isempty(argStruct.outArgs)
outputNames = strtrim(textscan(argStruct.outArgs, '%s', ...
'Delimiter', ','));
else
outputNames = {};
end
% Nested functions:
function str = strip_comments(str)
if strcmp(strtrim(str), '%{')
strip_comment_block;
str = strip_comments(fgets(fid));
else
str = strtok([' ' str], '%');
end
end
function strip_comment_block
str = strtrim(fgets(fid));
while ~strcmp(str, '%}')
if strcmp(str, '%{')
strip_comment_block;
end
str = strtrim(fgets(fid));
end
end
end
This is going to be very hard (read: impossible) to do for general functions (think of things like varargin, etc). Also, in general, relying on variable names as a form of documentation might be... not what you want. I'm going to suggest a different approach.
Since you control the program, what about specifying each module not just with the m-file, but also with a table entry with extra information. You could document the extra parameters, the function itself, notate when options are booleans and present them as checkboxes, etc.
Now, where to put this? I would suggest to have the main m-file function return the structure, as sort of a module loading step, with a function handle that points to the subfunction (or nested function) that does the real work. This preserves the single-file setup that I'm sure you want to keep, and makes for a much more configurable setup for your modules.
function module = divide_load()
module.fn = #my_divide;
module.name = 'Divide';
module.description = 'Divide two signals';
module.param(1).name = 'left';
module.param(1).description = 'left signal';
module.param(1).required_shape = 'columnvector';
% Etc, etc.
function [value, remain] = my_divide(left, right)
value = floor(left / right);
remain = left / right - value;
end
end
When you can't get information from a programming langauge about its contents (e.g., "reflection"), you have to step outside the language.
Another poster suggested "regular expressions", which always fail when applied to parsing real programs because regexps cannot parse context free langauges.
To do this reliably, you need a real M language parser, that will give you access to the parse tree. Then this is fairly easy.
Our DMS Software Reengineering Toolkit has an M language parser available for it, and could do this.
Have you considered using map containers?
You can write your functions along these lines . . .
function [outMAP] = divide(inMAP)
outMAP = containers.Map();
outMAP('value') = floor(inMAP('left') / inMAP('right'));
outMAP('remain') = inMAP('left') / inMAP('right') - outMAP('value');
end
...and call them like this ...
inMAP = containers.Map({'left', 'right'}, {4, 5});
outMAP = divide(inMAP);
...and then simply examine tha variable names using the following syntax...
>> keys(inMAP)
ans =
'left' 'right'
inputname(argnum) http://www.mathworks.com/help/techdoc/ref/inputname.html .