Assign output of h5disp to variable - matlab

I am using MATLAB R2012a to read an hdf5 data file using the the function h5disp. How can I assign the output to a variable or write it to disk? Is there a way to redirect the output in MATLAB? My goal is to read in many files and output a summary of their contents to disk in a format equal to or similar to h5disp.
Like
h = output(h5disp(myhdf5file))
or to disk like
h5disp(myhdf5file) > outputfile.txt

You can use h5info to store that information in the workspace i.e.
h = h5info(myhdf5file);
or you can use diary to write the output of h5disp to a text file.
diary outputfile.txt
h5disp(myhdf5file)
diary off

Related

How to append a file to an existing zip file/folder

Is there a way to append a file to an existing zip file/folder?
zip(zipfilename,filenames) would overwrite the original zipfilename.
I am using Windows 10 in case that makes a difference.
system(['powershell.exe -ExecutionPolicy Bypass Compress-Archive -update ',...
filename,' ',...
zipfilename]);
works.
Alternatively, as #Rotem pointed out, and in some situations more suitable, we can use Java's ZipOutputStream as long as we convert data to binary first.
If we start with a file test.csv, then the contained string can be read into an uint8 array by
filename = 'test.csv'; % change filename as needed.
fid = fopen(filename, 'r');
data = fread(fid , '*uint8');
fclose(fid );
If we start with data in a Matlab Workspace, we need to convert the data into the format we desire in the eventual output file and then convert it to binary. For example, if the desired compressed file is a .csv file, in other words, formatted text, we can prepare the string output with sprintf and convert it to the corresponding uint8 array such as
data = uint8(sprintf('%d, %d\r\n', [1,2;3,4]));
Following the above, one can use Java's ZipOutputStream to output to an existing zip file. Example below.
filename='test.csv'; % change filename of zip entry as needed
zipfilename='test.zip'; % change filename as needed
fos = java.io.FileOutputStream(zipfilename);
zos = java.util.zip.ZipOutputStream(fos);
ze = java.util.zip.ZipEntry(filename);
% zos.setLevel(9); % optional
zos.putNextEntry(ze);
zos.write(data, 0, numel(data));
zos.finish;
zos.close;
The benefit of this approach is that, if the data starts off in the current Matlab Worksapce, no additional file needs to be created, which would have required additional i/o use 3 times (in the above example, write test.csv, read csv, delete csv).
Another benefit is that the this method does not require powershell which is OS dependent -- not available in earlier Windows versions for example.
If the data starts off in an external file with Windows 10, as specified in the OP, then the 1st method is more expedient.
Note: Additional information on reading from the zip file or reading file list from zip file can be found here and here.

Reading a file from a different directory in matlab

My matlab function is in a folder that contains the main project and the other functions of the code. However, the data is stored in a folder withing the main one named "data" and inside the specific dataset that i want, for example "ded4" in this example. I can't figure out how to open the text file that I want without changing the file to the main folder. The code I have so far is:
function[Classify] = Classify(logDir)
%%%%logDir='ded014a04';
Directory = ['data/' logDir '/']
Filename = [logDir '-fixationsOffSet']
File_name = fullfile(Directory,Filename)
File = fopen(File_name,'r')
end
The code is in the 'dev' folder, I think my path is correct because when I do
open(File_name)
it opens.
Thanks for the help
If you want to open the file in the editor, use
open(File_name)
If you want to read data from the file, you can use
dlmread(File_name) % Read ASCII delimited file.
or
C = textscan(File,'FORMAT') % Read formatted data from text file or string.
or more low-level using fscanf, e.g., if the file contains three columns of integers you do the following: Read the values in column order, and transpose to match the appearance of the file: (from the help of fprintf)
fid = fopen('count.dat');
A = fscanf(fid,'%d',[3,inf])';
fclose(fid);

Matlab saving a .mat file with a variable name

I am creating a matlab application that is analyzing data on a daily basis.
The data is read in from an csv file using xlsread()
[num, weather, raw]=xlsread('weather.xlsx');
% weather.xlsx is a spreadsheet that holds a list of other files (csv) i
% want to process
for i = 1:length(weather)
fn = [char(weather(i)) '.csv'];
% now read in the weather file, get data from the local weather files
fnOpen = xlsread(fn);
% now process the file to save out the .mat file with the location name
% for example, one file is dallasTX, so I would like that file to be
% saved as dallasTx.mat
% the next is denverCO, and so denverCO.mat, and so on.
% but if I try...
fnSave=[char(weather(i)) '.mat'] ;
save(fnSave, fnOpen) % this doesn't work
% I will be doing quite a bit of processing of the data in another
% application that will open each individual .mat file
end
++++++++++++++
Sorry about not providing the full information.
The error I get when I do the above is:
Error using save
Argument must contain a string.
And Xiangru and Wolfie, the save(fnSave, 'fnOpen') works as you suggested it would. Now I have a dallasTX.mat file, and the variable name inside is fnOpen. I can work with this now.
Thanks for the quick response.
It would be helpful if you provide the error message when it doesn't work.
For this case, I think the problem is the syntax for save. You will need to do:
save(fnSave, 'fnOpen'); % note the quotes
Also, you may use weather{i} instead of char(weather(i)).
From the documentation, when using the command
save(filename, variables)
variables should be as described:
Names of variables to save, specified as one or more character vectors or strings. When using the command form of save, you do not need to enclose the input in single or double quotes. variables can be in one of the following forms.
This means you should use
save(fnSave, 'fnOpen');
Since you want to also use a file name stored in a variable, command syntax isn't ideal as you'd have to use eval. In this case the alternative option would be
eval(['save ', fnSave, ' fnOpen']);
If you had a fixed file name (for future reference), this would be simpler
save C:/User/Docs/MyFile.mat fnOpen

MATLAB: Use a variable with a file extenstion to load files

I am using MATLAB
I Have 51 files in their own directory all of .out extention created by a seperate program, all numbered 0 to 50.
ie
0.out
1.out
2.out
and so on til 50.out.
I need to load each file 1 by one to do calculations upon them within a for loop. How would I do this using the count variable to load the file, if the directory is set beforehand?
i.e.
%set directiory
cd(......)
%for loop
For count = 0:50,
data = count.out *<-----this line*
.....
Many thanks!
First generate the file name with
fileName = [int2str(count) '.out'];
then open the file with
fid = fopen(fileName, 'r');
The loading phase depends on the kind of file you want to read. Assuming it is a text file you can, for example, read it line after line with
while ~feof(fid)
line = fgetl(fid);
end
or use more specialized functions (see http://www.mathworks.it/it/help/matlab/text-files.html). Before the end of the for loop you'll have to close the file by calling
fclose(fid);
Another quite nice way to do it is to use the dir function
http://www.mathworks.co.uk/help/matlab/ref/dir.html
a = dir('c:\docs*.out')
Will give you a structure containing all the info about the *.out files in the directory you point it to, (or the path). You can then loop through it bit by bit. using fopen or csvread or whatever file reading function you want to use.

How to load .mat file which has a variable filename?

%select all .mat files
oar = dir('*oar.mat'); n = {oar.name};
%loop through files
for l=1:length(oar);
load pat_oar(l) %<---this is the .mat file with variable filename
clear ...
end
How do I write some Matlab script that will read in one .mat file after another...
You file names are stored in n, so you should be able to do:
for l=1:length(oar)
load(n{l})
end
Use the functional form. Instead of:
load pat_oar{I}
Use
load(pat_oar{I})
Calling a Matlab command using the unix-style syntax (i.e. command arg1 arg2) is just syntax shorthand for the more verbose syntax of command('arg1','arg2'). It's a pretty common trick to use the more verbose syntax anytime an argument is stored in a variable.