I am trying to write a MATLAB function that extends the "save" command to create the specified path if it doesn't already exist. I have been successful with a single variable, but not with multiple variables. With multiple variables, I receive the following error message:
31 variables_to_save(i) = variable_name;
Conversion to cell from char is not possible.
Error in saveMatVars (line 31)
variables_to_save(i) = variable_name;
Here is my function:
function saveMatVars( file_path, varargin )
%saveMatVars saves variables in varargin to file specified in file_path, creating the file path if it doesn't exist
% varargin is the variables to save
% get path and filename from file_path
[parent_folder, ~] = fileparts(file_path);
% if parent_folder doesn't exist, create it
if ~exist(parent_folder, 'dir')
mkdir(parent_folder);
end
% get names of variables to save
num_vars = length(varargin);
switch num_vars
case 0
return
case 1
variable_name = inputname(2);
variable_name = matlab.lang.makeValidName(variable_name);
variable_val = varargin{1};
feval(#()assignin('caller', variable_name, variable_val)); % create
a dummy function so I can access the current function's workspace
variables_to_save = variable_name;
otherwise % note - this part does not work
variables_to_save = cell(1,1);
for i = 1:num_vars
variable_name = inputname(i+1);
variable_name = matlab.lang.makeValidName(variable_name);
variable_val = varargin{i};
feval(#()assignin('caller', variable_name, variable_val)); % create a dummy function so I can access the current function's workspace
variables_to_save(i) = variable_name;
end
variables_to_save = string(variables_to_save);
end
% save variables_to_save in file_path
save(file_path, variables_to_save);
return
end
The documentation on "save" says the following about the variables to save:
save(filename,variables) saves only the variables or fields of a structure array specified by variables.
I have tried various functions for converting char arrays to strings and to cell arrays to no avail.
Related
I am trying to create an array of structures in Simulink and got some problems with it.
first of all i tried to create it directly in Simulink using this:
function a = fcn(Dibhole, t , x, const)
%#codegen
%Output = zeros(10,10);
f1 = 'number';
f2 = 'move';
cube = struct(f1, 0, f2, 0);
a = repmat(cube, 20, 10);
for i = 1:20
for j = 1:10
a(i,j).number = 0;
a(i,j).move = 0;
end
end
and i got this error:
Derived output was of type struct. 'Inherited type' is unsupported for this
type and a defined bus object must be used instead. Click on 'a' and
set data type for 'a' to be 'Bus: ', where '' is
the name of a bus object from the MATLAB workspace.
So i found some example how to create struct in Matlab and receive this to Simulink: http://blogs.mathworks.com/seth/2011/12/05/initializing-buses-using-a-matlab-structure/
That works perfectly but i still can't repeat this with array:
f1 = 'number';
f2 = 'move';
cube = struct(f1, 0, f2, 0);
myStruct2 = repmat(cube, 20, 10);
for i = 1:20
for j = 1:10
myStruct2(i,j).number = 1;
myStruct2(i,j).move = 1;
end
end
busInfo = Simulink.Bus.createObject(myStruct2);
Can anyone clarify to me what's the problem? Or maybe there is different way to create array of struct in Simulink?
Mihail
Simulink wants you to define the output of the function to be a bus.
As 'Bus: My_test_bus', for example.
Take a look at the Simulink Bus Editor. You can find it in any model under the menu, Edit->Bus Editor.
This would be a good start.
Rick, i think you are right!
i have tried this problem for a long time, and have got this results:
the irony is that I was never able to create array of structures BUT i did this with structure of arrays! :D
I made this steps for it:
to use structure of arrays we need to define and initialize it in some MATLAB function. Like this:
number = zeros(10,1);
move = zeros(10,1);
for i = 1:10
number(i,1) = i+1;
move(i,1) = i+2;
end
a = struct('numbers',number,'movement', move);
To work this data we must use Bus Selector.
So we have array in "numbers" and "movement".
BUT! Here we go, Rick: we must define type of output of MATLAB function like Bus! How to do this in simulink? i found this way:
in model properties in simulink Callbacks/PreLoadFcn define some function and in same folder as project create .m file named like this just defined function.
In this file create structure of array and define Bus type for it:
number = zeros(10,1);
move = zeros(10,1);
a = struct('numbers',number,'movement', move);
busInfo = Simulink.Bus.createObject(a);
Now we have Bus type for our structure at first loading of simulink model.
Last step: define MATLAB function output type directly.
in Model Explorer choose your MATLAB function. choose output variable. Set DataType for it: Bus:slBus1 (the name of this Bus type you can see in wokspace of matlab, because its a global variable).
That's all! now it works!
(tried to add pictures, but i have no enough reputation :( )
Now my program works in this way, but i also tried to create array of structures and still have the problems. i tried to create Bus for it, but can't transmit it to Bus Selector - it doesn't know what to do with structures... i also tried to add one more MATLAB function to create some data from structures and then display it, but it doesn't works too(
I have put some values in a file. In the puppet manifests I want to get the value of these variables. Is there a specific directory to put my file? Also, what should be the format of the file?
If this file on puppet-server, you can write functions for module. For example:
modules/my_module/lib/puppet/parser/functions/get_var.rb:
$: << File.expand_path(File.join(File.dirname(__FILE__), '.'))
module Puppet::Parser::Functions
newfunction(:get_var,
:type => :rvalue) do |args|
file_name = args[0]
f = File.open(file_name)
s = f.readline()
return s
end
end
And use it in manifests: $test = get_var('/etc/puppet/configs.txt'). This function return first string from file, but you can change it for your needs.
For file on client, you can write facter.
Is there a way to create a Simulink bus from the definition of a C struct? Let's say I have some C struct definition in a header file:
typedef struct {
double a, b;
} u_T;
Can I use this to automatically generate a Simulink.Bus object?
Edit: Is there a tool that generates Matlab code for creating Simulink.Bus objects describing the structs from a .h file?
This is supported in the latest version of MATLAB (2017a). Use the following command.
importInfo = Simulink.importExternalCTypes(headerFiles)
For more info see: https://www.mathworks.com/help/simulink/slref/simulink.importexternalctypes.html
You can import a header when creating a bus object, but this is only used for code generation with Simulink Coder, not for normal simulation with Simulink. See the documentation on Simulink.Bus for more details.
The only way to do what you want would be to write a parser that reads your .h file and creates a bus object in the MATLAB workspace. I don't know of any such tool I'm afraid.
Here is a MATLAB script:
filename = 'Test3.h';
fid = fopen(filename);
tline = fgetl(fid);
i = 1;
while ischar(tline)
% Search for structs
if any(strfind(tline,'typedef struct'))
tline = fgetl(fid);
% Get elements
while ~any(strfind(tline,'}'))
% Create Element
el(i) = Simulink.BusElement;
c = regexp(tline,'(\w*) (\w*);','tokens');
% Element Name
el(i).Name = c{1,1}{1,2};
% Element Data type
switch c{1,1}{1,1}
case 'float'
el(i).DataType = 'single';
case 'int8_t'
el(i).DataType = 'int8';
case 'uint32_t'
el(i).DataType = 'uint32';
% Add unknown Data types as cases here
otherwise
el(i).DataType = ['Bus: ',c{1,1}{1,1}];
end
i = i+1;
tline = fgetl(fid);
end
% Get struct name
c = regexp(tline,'} (\w*);', 'tokens');
% Create bus
eval([c{1,1}{1,1},' = Simulink.Bus;']);
% Assign elements
eval([c{1,1}{1,1},'.Elements = el;']);
% Assign Header file
eval([c{1,1}{1,1},'.HeaderFile = ''',filename,''';']);
clear el; i = 1;
end
tline = fgetl(fid);
end
fid = fclose(fid);
clear c fid filename i tline;
I hope it helps...
I know this is a simple question, but for some reason I can't find a straight answer that works no matter where I look.
Basically, I have 4 values that were found in one m file, and I want to run them through a separate m file and retrieve the output from it.
I tried something like these, but none worked:
result = generate(nrow,ncol,a,b);
function result = generate(nrow,ncol,a,b);
result = #generate(nrow,ncol,a,b);
The final value in the m file "generate" is called result, and I'm trying to carry that across to my initial m file.
Any advice as to what I'm doing wrong would be greatly appreciated! Please and thank you
if your file generate.m defines a function it should have itself the following structure (which takes into account the fact that you have four returned values)
function [ret1 ret2 ret3 ret4] = generate(nrow,ncol,a,b)
.... % # Some processing of yours
ret1 = ... ; % # Returned values are eventually set
ret2 = ... ;
ret3 = ... ;
ret4 = ... ;
end
The function should be called (e.g. in your main script) as
[ret1 ret2 ret3 ret4] = generate(nrow,ncol,a,b);
now you have the variables ret1,ret2,ret3,ret4 available in the caller scope.
Be aware that the file generate.m must be in the current matlab PATH.
I was wondering how do I get a line into an array with lua in some sort of function
eg. FileToArray("C:/file.txt")?
I know I can use:
var = io.open("file")
Data = var:read()
But it only returns the 1st line, and no other lines.
Anyone know how to fix this or a different way? I'm new to lua and the file system stuff.
You can pass "*a" to read function, it should read the whole file:
local file = io.open("file-name", "r");
local data = file:read("*a")
And if you want to store each line in an array. Like Jane's solution you can use
io:lines () - which returns iterator function (each call gives you a new line)
local file = io.open("file-name", "r");
local arr = {}
for line in file:lines() do
table.insert (arr, line);
end
local file = io.open("c:\\file.txt")
local tbllines = {}
local i = 0
if file then
for line in file:lines() do
i = i + 1
tbllines[i] = line
end
file:close()
else
error('file not found')
end
See: http://lua-users.org/wiki/IoLibraryTutorial for more information.