Loading a textfile in FreeMat - matlab

I am trying to get my MATLAB code for loading and parsing certain text files to work in FreeMat and have run into difficulties on square one.
The file looks like this:
1110000
1001100
1000011
0101010
0100101
0011001
0010110
Number of 7-3-1 designs = 1 Number of designs with |Aut|= 168
is 1
textread.m seems to be unknown to FreeMat and calls to load and dlmread haven't done the job either.

Looks like textscan/textread are not yet implemented in FreeMat. You can instead read the file line-by-line using fgetline (equivalent to fgets in MATLAB).
Here is a list of available IO functions in FreeMat

Related

Post-processing the output result from Dymola when there are only 2 elements in the result

I am trying to use the MATLAB scripts shipped with Dymola to post-process the output result of Dymola. But in some cases, the output data in the .mat file only have 2 elements, how could I get the data between 10s and 100s in this kind of cases?
It's a parameter or variable that is not time depending so it's stored in a compact way. I understand the mechanism, but it is not user-friendly when post-processing the data in MATLAB, I have to find the "wrong" dimensional data. How could I fix this issue?
I recommend creating some simple logic that looks at the size of the variable and then automatically puts it into some dictionary, list, etc. From there you can manipulate the variable. I know you are asking for Matlab but here is a Python solution that I have used which may help you get started:
varNames_param_base=[]
varNames_var_base=[]
for i, val in enumerate(r.varNames()):
if np.size(r.values(val)) == 4:
varNames_param_base.append(val)
else:
varNames_var_base.append(val)
I used those lines in this file.
In the example r.varNames() is a list of all the variable names (i.e., strings) which are read from the resulting Dymola .mat file. r.values gets the value of the variable name currently being used in the for loop (i.e., val).
You may also consider converting your result file to SDF (a simple HDF5 representation), because that format does not use any clever storage options (if I remember correctly).

Matlab s2p file

I'm trying to build a s2p file in simulink using the block "To File" but gives me an odd file with random characters like
fs%($&%%(&
Which looks like when I try to open a jpeg file with block note.
I'm trying a simple RF layout with a single resistive divider and the input and output ports like in figure
anyone knows what is happening here?
The To File block writes a .mat (binary) data file. Opening one as a text file is always going to show odd random characters
To see what's in the file you need to use load to load the saved signal(s) into the MATLAB Workspace.

how to append to a file using scala/breeze library

I wish to write to a file the output row of result matrix (produced in iterations) so that I can support checkpointing.
I figured we can use the csvwrite command to write the entire matrix to a file but how can I append to a file?
I am looking for something like below:
breeze.linalg.csvwrite(new File("small.txt"),myMatrix(currRow,::).t.asDenseMatrix)
However the above command overwrites the file each time the command is executed.
There's nothing built-in to Breeze. (Contributions welcome!)
you can use breeze.io.CSVWriter.write directly if you would like.

How to convert a .txt to .cdf ? Needed for Matlab that won't read txt as cdf

I have a txt file that contains data in Common Data Format( CDF ).
Matlab has functions to read this but it does not work. I assume because the extension is .txt and not .cdf.
When I try to read it I get:
??? Error using ==> cdfinfoc
Error issued from CDF library: "NOT_A_CDF_OR_NOT_SUPPORTED: Named CDF is corrupted or
not supported by the current library version."
Error in ==> cdfinfo at 170
tmp = cdfinfoc(filename);
Error in ==> cdfread at 184
info = cdfinfo(filename);
Is there a way to trick Matlab and read it or do I need to transform somehow the txt into a .cdf? If so, how do I do that?
Thank you!
EDIT: The file that I am trying to read is from this link: http://www.ee.washington.edu/research/pstca/pf14/ieee14cdf.txt
This says that the data is in CDF: http://www.ee.washington.edu/research/pstca/pf14/pg_tca14bus.htm
If it's actually a txt file (in that it contains textual data that you can read), then it isn't really in CDF format. You can try makeCDF or some other tool to convert the textual data into a CDF file.
If that doesn't work out for you, you'll need to post more information about the actual format of the file. That text file could contain anything. Maybe provide some example lines?
EDIT
After looking at your file, this is an unrelated format which happens to also be called CDF. You can find a reader for this format here: Read IEEE Common Data Format (CDF) (Power systems, Load Flow)
In both cases I think where it's actually falling over is this call to the library:
fmt = cdflib.getFormat(cdfid);
See: cdflib.getFormat, in particular these lines:
This function corresponds to the CDF library C API routine
CDFgetFormat.
To use this function, you must be familiar with the CDF C interface.
Read the CDF documentation at the CDF Web site.
At any rate it's not due to the file extension alone; I tested this by making a copy of the MATLAB example.cdf, renaming it to example.txt, and calling cdfinfo on both. No errors, returned data is the same except for obvious things like filename/modification date.

MATLAB - Stitch Together Multiple Files

I am new to MATLAB programming and some of the syntax escapes me. So I need a little help. Plus I need some complex looping ideas.
Here's the breakdown of what I have:
12 seperate .dat files, each titled something like output_1_x.dat, output_2_x.dat, etc.
each file is actually one piece of a whole that was seperated and processed
each .dat file is approx. 3.9 GB
Here's what I need to do:
create a single file containing all the data from each seperate file, i.e. I need to recreate the original file.
call this complete output file something like output_final.dat
it has to be done in MATLAB, there are no other alternatives (actually there maybe; see note below)
What is implied:
I will have to fread each 3.9 GBfile into chunks or packets, probably 100 mb at a time (using an imbedded loop?)
these packets will have to be read then written sequentially
after one file is read then written into output_final.dat, the next file is automatically read & written (the master loop).
Well, that's pretty much it. I did a search for 'merging mulitple files' and found this. That isn't exactly what I need to do...I don't need to take part of a file, or data from files, and write it to a new one. I'm simply...concatenating...? This would be simple in Java or Perl, but I only have MATLAB as a tool.
Note: I am however running KDE in OpenSUSE on a pretty powerful box. Maybe someone who is also an expert in terminal knows a command/script to do this from the kernel?
So on this site we usually would point you to whathaveyoutried.com but this question is well phrased.
I wont write the code but i will give you how I would do it. So first I am a bit confused about why you need to fread the file. Are you just appending one file onto the end of another?
You can actually use unix commands to achieve what you want:
files = dir('*.dat');
for i = 1:length(files)
string = sprintf('cat %s >> output_final.dat.temp', files(i).name);
unix(string);
end
That code should loop through all the files and pipe all of the content into output_final.dat.temp (then just rename it, we didn't want it to be included in anything);
But if you really want to use fread because you want to parse the lines in some manner then you can use the same process:
files = dir('*.dat');
fidF = fopen('output_final.dat', 'w');
for i = 1:length(files)
fid = fopen(files(i).name);
while(~feof(fid))
string = fgetl(fid) %You may choose to parse the string in some manner here
fprintf(fidF, '%s', string)
end
end
Just remember, if you are not parsing the lines this will take much much longer.
Hope this helps.
I suggest using a matlab.io.matfileclass objects on two of the files:
matObj1 = matfile('datafile1.mat')
matObj2 = matfile('datafile2.mat')
This does not load any data into memory. Then you can use the objects' methods to sequentialy save a variable from one file to another.
matObj1.varName = matObj2.varName
You can get all the variables in one file with fieldnames(mathObj1) and loop through to copy contents from one file to another. You can then clear some space by removing the copied fields. Or you can use a bit more risky procedure by directly moving the data:
matObj1.varName = rmfield(matObj2,'varName')
Just a disclaimer: haven't tried it, use at own risk.