I would like to load different files that have more or less the same just a parameter (temperature) is changing.
For example:
detector_temp = importdata('beginning name file' temp 'K_end name file.dat');
I don't know how to make different "detector" files for all my temperatures.
For the moment I just write all the different file names. I have plenty.
Thank you in advance
Need more information to provide an exact solution but generally you can use dir to get the filenames you want and then load the data into a cell or some other container.
For example say I have a directory of mat files I want to load.
% from the desired directory
files = dir('*.mat'); % get the files I want to load
fileNames = {files.name}'; % extract just the file name
data = cellfun(#load, fileNames, 'UniformOutput', 0); % load all the files into a cell
You can then work on the data that is stored in the cell. Each file is stored in a separate cell. So file1 = data{1,1};, file3 = data{3,1} and so on.
Related
I have many .txt files that contain n rows and 7 columns each delimited with whitespace. I want to convert each file to .mat file and save that in the same folder.
I tried this but it's not working:
files = dir('*.txt');
for file = files'
data=importdata(file.name);
save(file.name, 'data');
end
While this works for a single file, i want to do it programmably since the number of .txt files i have is very large:
data=importdata('myfile.txt');
save('myfile', 'data');
Thank you for your help
This should work
files = dir('*.txt');
for idx = 1:length(files)
file_name = files(idx).name;
fprintf("Processing File %s\n",file_name);
data=importdata(file_name);
[filepath,name,ext] = fileparts(fullfile(pwd,file_name));
save([name '.mat'],'data');
end
dir creates a stucture which you need to index through so we create the for loop to start at 1 and keep going until all the elements of dir have been processed.
Note in the code, I've also added a section to split the file name (e.g file1.txt) in to the file name and extension. This is so we only use the name part and not the extension when creating the mat file.
#scotty3785's answers worked well and also this worked for me in case somebody needs it:
files = dir('*.txt');
for i=1:length(files)
data=importdata(files(i).name);
save(erase(files(i).name,".txt"), 'data');
end
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);
I have a directory with two folders: one folder contains several subfolders with multiple .txt files (folder 1). The second folder contains several .hjson files (folder 2).
I would like load each .txt and .hjson files make a several calculation (e.g. velocity, acceleration, curvature) and save in the same .txt file adding news columns with headers (e.g. velocity, acceleration, curvature). So far, I have one code to load .txt files.
My goal is to make a code that reads, loads, computes and automatically save it. Please let me know if you have any suggestion.
%% Read and load
dir_to_search = 'C:\Programs\pedro\Test\';
txtpattern = fullfile(dir_to_search, '*.txt');
dinfo = dir(txtpattern);
for K = 1 : length(dinfo)
thisfilename = fullfile(dir_to_search, dinfo(K).name); %just the name
thisdata = load(thisfilename); %load just this file
End
You can use fprintf to write to a file.
For example:
formatSpec="%9.5f %8.5f\n";
for i=1:n
fprintf(fid,formatSpec, var1(i),var2(i));
end
I want to read multiple files from a folder but this code does not work properly:
direction=dir('data');
for i=3:length(direction)
Fold_name=strcat('data\',direction(i).name);
filename = fullfile(Fold_name);
fileid= fopen(filename);
data = fread (fileid)';
end
I modified your algorithm to make it easier
Just use this form :
folder='address\datafolder\' ( provide your folder address where data is located)
then:
filenames=dir([folder,'*.txt']); ( whatever your data format is, you can specify it in case you have other files you do not want to import, in this example, i used .txt format files)
for k = 1 : numel(filenames)
Do your code
end
It should work. It's a much more efficient method, as it can apply to any folder without you worrying about names, number order etc... Unless you want to specify certain files with the same format within the folder. I would recommend you to use a separate folder to put your files in.
In case of getting access to all the files after reading:
direction=dir('data');
for i=3:length(direction)
Fold_name=strcat('data\',direction(i).name);
filename = fullfile(Fold_name);
fileid(i)= fopen(filename);
data{i-2} = fread (fileid(i))';
end
I have a data set of big number of videos so, I want to read these videos and save each video separately with its name because it consumes a lot of time to process among all these videos every time specially for training and classification. If you have any idea how can read all video files in the specified folder D:\words of format .avi and save each one with its own name as .MAT file.
But this code doesn't work
Thanks,,,
files = fuf('D:\words');
for i = 1:size(files);
name = files{i};
file = strcat('D:\words',name);
x = VideoReader(file.avi); %NOT SURE FROM THIS LINE%
v = read(x)
name = strcat(name,'.mat');
save(name,'v');
end
You don't need an additional function like fuf to get a list of file names.
If all your files are in "D:\words" (i.e. not in a bunch of sub-directories, which would complicate things), you can just use things like ls to fetch a list of all the avi files.
This is not the most elegant way of doing it (hard coding the directory and not using things like fullfile), but hopefully it's relatively easy to understand what's going on:
% use ls or dir to specifically match *.avi files
files = ls('D:\words\*.avi')
% note that size can return more than one value
% hence size(files,1)
for n = 1:size(files,1);
filename = files(n,:); % pick one file
% assuming this works - you might want to do some error checking
x = VideoReader(filename);
v = read(x);
% now we just want the name minus the ext
[pathstr,name,ext] = fileparts(filename);
fout = ['D:\words\',name,'.mat'];
save(fout,'v');
end
Your variable file is likely a string, not a structure:
...
file = strcat('D:\words',name);
x = VideoReader(file);
...
Or maybe this if the files in your cell array don't have extensions:
...
file = strcat('D:\words',name);
x = VideoReader([file '.avi']);
...
If your fuf function returns files that are not AVI movies, you'll need to do more work.