Combine data from multiple .log files and reading them without creating new .txt file in MATLAB - matlab

At the moment I have working code that can read multiple .log files and create a giant .txt file which contains all their data. Then I simply read the giant .txt file. However, this code is inefficient because it involves writing a new .txt file every time I want to run the script.
Does someone know how to simplify this so that it doesn't write a new .txt file every time? Ideally by creating a 'virtual' .txt file?
files=dir('IndividualFiles.*.log');
fileout='GIANT.txt';
fout=fopen(fileout,'w');
for cntfiles=1:length(files)
fin=fopen(files(cntfiles).name);
temp = fread(fin,'uint8');
fwrite(fout,temp,'uint8');
fprintf(fout,'\n');
fclose(fin);
end
fclose(fout);
filename = 'C:\Users\myname\Desktop\Learning Documents\myfolder\GIANT.txt';

Related

How to convert group of .dat file has 16bit integer data to group of .txt file?

I have a group of .dat files I need to convert to .txt files. I have a directory called "data" that has "210" files (0.dat, 1.dat, ......210.dat), I want to convert these .dat files to .txt files (0.txt, 1.txt ......210.txt), the data type is 16bit integer.
Ideally you are supposed to try a few steps before coming here asking for a solution. Since this is your first post, I'll give you a few pointers. Next time please Google a few solutions. Try them. Post your code/error if you have any issues to receive help.
From the directory, Load all the contents of the folder using,
files = dir('*.dat');
Add a FOR loop to read each dat file one by one.
fileID = fopen('XXXX.dat');
OneByte = fread(fileID,'*uint16');
Then once the file is loaded you can convert it into .txt file.

MATLAB script that can call .mat files without directory

I have a MATLAB script and a lot of folders that are called in that script. I am sending out the script, which is part of a publication and would like to make the script freely accessible along with the .mat files. I was wondering if there was an easy way to do this where the user can just run the script and the files can be called from the script. So it's like a software that just calls the .mat files rather than a code that the user needs to read and understand to call the files.
Thanks!
You have a couple of options.
Determine the directory dynamically and use that to load the .mat files (preferred)
thisdir = fileparts(mfilename('fullpath'));
matpath = fullfile(thisdir, 'subdirectory', 'file.mat');
data = load(matpath);
Put the folder containing the .mat files on the PATH and then load them with just the name
addpath('/folder/containing/mat/files')
data = load('file.mat');
Have the user select the files using uigetfile
[fname, pname] = uigetfile();
filename = fullfile(pname, fname);
data = load(filename);

How to read paths from txt and input the files in Matlab?

I have one txt file whitch contains paths to a lot of other txt files of data in my project , i am quite new to matlab , so i cant figure out a way how to add the first txt file with the paths and make matlab read all the paths and add all the other txt files ?
Any help is apriciated , thanks for your time :)

Extracting specific file from zip in matlab

Currently I have a zipfile containing several thousand .xml files, extracted the folder is 1.5gb in size.
I have a function that matches data with specific files inside this zip file. I then want to read this specific file and extract additional data.
My question:
Is there any way to extract these specific files from the archive without unzipping the entire archive?
The built in unzip.m function can only be used to unzip the entire file so it won't work so I am thinking I have to use the COM interface or some other approach.
Matlab version: R2013a
While searching for solutions I found this:Read the data of CSV file inside Zip File without extracting the contents in Matlab
But I can't get the code in the answer to work for my situation
Edit:
Credit to Hoki and Intelk
zipFilename = 'HMDB.zip';
zipJavaFile = java.io.File(zipFilename);
zipFile=org.apache.tools.zip.ZipFile(zipJavaFile);
entries=zipFile.getEntries;
cnt=1;
while entries.hasMoreElements
tempObj=entries.nextElement;
file{cnt,1}=tempObj.getName.toCharArray';
cnt=cnt+1;
end
ind=regexp(file,'$*.xml$');
ind=find(~cellfun(#isempty,ind));
file=file(ind);
file = cellfun(#(x) fullfile('.',x),file,'UniformOutput',false);
And not forgetting the
zipFile.close

How to run the same code with many files (different file name in same directory) in Matlab?

I have a thousand .dat files to run with the same program. Is there any faster way or script to run it automatically instead of run them one by one? The .dat files have different filenames.
The program is something like:
fid=fopen('**abd**.dat');
C=textscan(...);
...
save('**abd**.txt',data);
The abd is the file name. I have thousands of files with different file names. It is a bit annoying by keep copying and pasting those filenames into the program and run it. Anyone got a faster way or code for this?
you can use "dir" to get a list of files, and then process them in a loop like this.
fns = dir('*.dat');
for i = 1:length(fns)
fid = fopen(fns(i).name);
C = textscan(...);
fclose(fid);
save([fns(i).name,'.dat'],data);
end
Rethink the problem. Write one script to read a text file of file names and strings. Then you've got 2 files, not thousands.