creating .txt file from S function - simulink

I have an C mex S function which can print output to the .txt file.
This code inside mdlStart(SimStruct *S) creates datafile.txt file and appends the output.
void** pwork = ssGetPWork(S);
FILE *datafile;
datafile = fopen("datafile.txt","a");
pwork[0] = datafile;
So now I want to dynamically get the filename of .txt file from user through S function mask . so , How can I receive string value of filename from mask inside S function.

First, yo will need to declare the mask parameter
the, you will need to pass the filename to SFunction parameter
Last, you will use the parameter inside the sfunction, like this:
mxGetPr(ssGetSFcnParam(S,0));
See:
http://www.mathworks.com/help/simulink/sfg/ssgetsfcnparam.html
http://www.mathworks.com/matlabcentral/answers/36028-integer-parameters-to-c-mex-s-function

Related

Save a workspace with current time. MAtlab

I would like to save all simulation variables ad fig with the current time.
my solution:
t = datetime('now','Format','dd-MM-yyyy''_T''HHmmss');
t2 = datevec(t);
DateString = datestr(t2);
filename=[DateString,' all_variables_main '];
save(filename )
savefig(filename)
The following error was given in Matlab:
Unable to write file 26-Oct-2019 09:47:15 all_variables_main : Invalid argument.
What have I done wrong?
mat filenames cannot have spaces or colons in them. You can use the following to directly obtain the date and time in a format that is allowed in a filename:
>> fileName = [datestr(now, 'dd-mmm-yyyy_HHMMSS') '_all_variables_main']
fileName =
'26-Oct-2019_103123_all_variables_main'
>> save(fileName)
File name containing : character is not a valid file name.
You can replace the : with "꞉" character.
See: How to get a file in Windows with a colon in the filename?
You can replace all : with ꞉ character (unicode character A789 that looks like colon) that is valid to be used in file name.
filename(filename == ':') = char(hex2dec('A789'));
Make sure to use the right character when loading the file.
Remark: The above solution was tested in Windows 10, and MATLAB R2016a.

Double Quotes on VarNames Matlab

I am reading a comma-separated text file into Matlab with the dataset() function. The variable names in the text file contain double quotes around the names. I am reading this file into Matlab, manipulating the data, and exporting to a separate text file. The issue is that I need the double quotes to remain around the variable names; however, Matlab removes them.
Is there a way I can tell Matlab to keep the quotes, or an easy way to replace the quotes before exporting? Thanks
Text file looks like this: ["Var1","Var2",....] - There is no issue with Matlab importing the file.
inputname = 'MyFile.TXT';
outputname = 'MyOutput.TXT';
rawinventory = dataset('File', inputname', 'Delimiter', ',',
'ReadVarNames', true);
rawinventory(1,1); %The Command Window shows the first entry and the
%header name without the double quotes.
temp_raw = dataset2cell(rawinventory(:,:));
% Perform some data Manipulation here
%........
edited_raw = cell2dataset(temp_raw(:,:));
export(edited_raw, 'File', outputname, 'Delimiter', ',');
I know that there may be better ways to run this code. My job is not as a developer or IT. I occasionally need to edit files used in other processes. Unfortunately, the process after this manipulation requires the double quotes around the variable names.
This function solves this issue. Place the .m file for the function in the same folder as the .m file with the code. https://www.mathworks.com/matlabcentral/fileexchange/25387-write-cell-array-to-text-file?s_tid=gn_loc_drop
inputname = 'MyFile.TXT';
outputname = 'MyOutput.TXT';
rawinventory = dataset('File', inputname', 'Delimiter', ',',
'ReadVarNames', true);
rawinventory(1,1); %The Command Window shows the first entry and the
%header name without the double quotes.
temp_raw = dataset2cell(rawinventory(:,:));
for y = 1:size(temp_raw,2);
temp_raw(1,y) = strcat('"',temp_raw(1,y),'"');
end %This for loop replaces the double quotes
% Perform some data Manipulation here
%........
dlmcell(outputname,temp_raw,',');

Saving figure without providing filename [duplicate]

this question about matlab:
i'm running a loop and each iteration a new set of data is produced, and I want it to be saved in a new file each time. I also overwrite old files by changing the name. Looks like this:
name_each_iter = strrep(some_source,'.string.mat','string_new.(j).mat')
and what I#m struggling here is the iteration so that I obtain files:
...string_new.1.mat
...string_new.2.mat
etc.
I was trying with various combination of () [] {} as well as 'string_new.'j'.mat' (which gave syntax error)
How can it be done?
Strings are just vectors of characters. So if you want to iteratively create filenames here's an example of how you would do it:
for j = 1:10,
filename = ['string_new.' num2str(j) '.mat'];
disp(filename)
end
The above code will create the following output:
string_new.1.mat
string_new.2.mat
string_new.3.mat
string_new.4.mat
string_new.5.mat
string_new.6.mat
string_new.7.mat
string_new.8.mat
string_new.9.mat
string_new.10.mat
You could also generate all file names in advance using NUM2STR:
>> filenames = cellstr(num2str((1:10)','string_new.%02d.mat'))
filenames =
'string_new.01.mat'
'string_new.02.mat'
'string_new.03.mat'
'string_new.04.mat'
'string_new.05.mat'
'string_new.06.mat'
'string_new.07.mat'
'string_new.08.mat'
'string_new.09.mat'
'string_new.10.mat'
Now access the cell array contents as filenames{i} in each iteration
sprintf is very useful for this:
for ii=5:12
filename = sprintf('data_%02d.mat',ii)
end
this assigns the following strings to filename:
data_05.mat
data_06.mat
data_07.mat
data_08.mat
data_09.mat
data_10.mat
data_11.mat
data_12.mat
notice the zero padding. sprintf in general is useful if you want parameterized formatted strings.
For creating a name based of an already existing file, you can use regexp to detect the '_new.(number).mat' and change the string depending on what regexp finds:
original_filename = 'data.string.mat';
im = regexp(original_filename,'_new.\d+.mat')
if isempty(im) % original file, no _new.(j) detected
newname = [original_filename(1:end-4) '_new.1.mat'];
else
num = str2double(original_filename(im(end)+5:end-4));
newname = sprintf('%s_new.%d.mat',original_filename(1:im(end)-1),num+1);
end
This does exactly that, and produces:
data.string_new.1.mat
data.string_new.2.mat
data.string_new.3.mat
...
data.string_new.9.mat
data.string_new.10.mat
data.string_new.11.mat
when iterating the above function, starting with 'data.string.mat'

Save data as modified input file name

I have a program that loads data from a .txt file and performs some curve fitting. The input file name for this example is experiment09.txt.
After processing I want to save a variable with the same input filename but appended with something like _fit. So my saved workspace variable in this case would be experiment09_fit.txt.
I have gotten this far in MATLAB:
buf = length(filename)
saveName = filename(1:buf-7)
which gives me a saveName of experiment09 but I am at a loss as to how to add my chosen string on the end to make it experiment09_fit. Once I have a valid save name I will then just call
save(saveName, 'fittedValue', '-ASCII');
Help would be greatly appreciated.
What about this:
filename = 'experiment09.txt';
[pathstr, basename, ext] = fileparts(filename);
outname = [basename, '_fit', ext]; % will give 'experiment09_fit.txt'
Also use string concatenation for adding additional names to string variables.
For example,
filename = 'experiment09.txt';
[pathstr, name, ext] = fileparts(filename);
outputName1 = strcat(name,'_fit.');
outputName = strcat(outputName1,ext);

How to put conversion operation in a for loop?

Below is the code to convert .tim file to ascii file for one particular file. But what I need is to convert 500 files(.tim). I also need to save the .ascii file in SAME name as the .tim file name like below for all 500 files.
bin=fopen('file_01.tim','r');
ascii = fread(bin, [43,21000], 'float32');
data_values=ascii';
dlmwrite('file_01.xls', data_values, 'delimiter', '\t', ...
'precision', '%.6f','newline','pc');
Using a "for loop" to do the conversion and save the ascii file with the same name of the tim, was my first thought but I don't know how to that.
You can use dir to get a list of all the filenames in your folder and then proceed just as you have but using replacing 'file_01.tim' with [D(ii).name]
e.g.
D = dir('*.tim');
for ii = 1:size(D,1)
bin=fopen(D(ii).name,'r');
%your processing etc
savename = [strtok(D(ii).name,'.'), '.xls']; %Change the file ext from .tim to .xls
dlmwrite(savename, ...