MATLAB - Load data file with a string file name - matlab-load

I am writing a Matlab program that loads a data file created in another C++ program.
planet = input('What is the name of your planet? ', 's')
data_file = strcat(planet, '.dat')
load(data_file);
data_file;
x = data_file(:,1);
y = data_file(:,2);
plot (x,y,'r*')
The program takes the name of the planet as the user input, then concatenates ".dat" to the end of the planet name. This gives, for example, "earth.dat," which is the name of the file created by the other C++ program.
I have made sure that the data file being loaded is in the correct folder; however, MATLAB still gives an error when I run the program.
What is the correct command for loading this file?
Thank you!

try using this instead:
planet = input('What is the name of your planet? ', 's')
filename=[num2str(planet) '.dat'];
data_file=load(filename);
x = data_file(:,1);
y = data_file(:,2);
plot (x,y,'r*')

Related

Running multiple files from with Scilab program

I'm new to Scilab. I have to run the same program with a dozen different input files. Currently I simply uncomment the line and then rerun the program, and change the output file to a new name
// Input data file
data_file = 'data1.txt';
//data_file = 'data2.txt';
//data_file = 'data3.txt';
//data_file = 'data4.txt';
//data_file = 'data5.txt';
//data_file = 'data6.txt';
etc. another 6 lines
// Output data file name
output_data = '/output_files/data1.csv';
Is there a way to read in each file (data1.txt, data2.txt...) execute the body of the program and then output a new output file (data1.csv, data2.csv ...) instead of what I'm doing now, which is running the program and then editing it to use the next file and run again?
Just do something like:
for i=1:6
// Input data file
data_file=msprintf("data%d.txt",i);
// Output data file name
output_data=msprintf("/output_files/data%d.csv",i);
// exec the body of your script
end

Is there a way to retrieve physiological data from acqknowledge and preprocess them in R?

I'm trying to retrieve raw physiological data (hand dynanometer) from Acqknowledge to be able to preprocess and analyze them in R (or Matlab) in an automatized way. Is there a way to do that ? I would like to avoid having to copy/paste the data manually from Acknowledge to Excel to read them in R.
Then I would like to apply a filter on the data and retrieve the squeezes of interest in R. Is there a way to do that ?
Any advice is very welcome, thank you in advance!
I had a similar problem. The key is the load_acq.m function, you can find it here -> https://github.com/munoztd0/fusy-octave-memories/blob/main/load_acq.m then you can just loop through them and save them as .csv or anything that you can load and work with in R.
As for how to do that I have put together a little routine to do that.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create physiological files from AcqKnowledge
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SETTING THE PATH
path = 'your path here'; %your path here
physiodir = fullfile(path, '/SOURCE/physio');
outdir = fullfile(path, '/DERIVATIVES/physio');
session = "first" %your session
base = [path 'sub-**_ses-' session '*'];
subj = dir(base); %check all matching subject for this session
addpath([path '/functions']) % load aknowledge functions
%check here https://github.com/munoztd0/fusy-octave-memories/blob/main/load_acq.m
for i=1:length(subj)
cd (physiodir) %go to the folder
subjO = char(subj(i).name);
subjX = subjO(end-3:end); %remove .mat extension
filename = [subjX '.acq']; %add .acq extension
disp (['****** PARTICIPANT: ' subjX ' **** session ' session ' ****' ]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% OPEN FILE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
physio = load_acq(filename); %load and transform acknowledge file
data = physio.data; %or whatever the name is
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CREATE AND SAVE THE CHANNEL
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cd (outdir) %go to the folder
% save the data as a file text in the participant directory
fid = fopen([subjX ',txt'],'wt');
for ii = 1:length(data)
fprintf(fid,'%g\t',data(ii));
fprintf(fid,'\n');
end
fclose(fid);
end
Hope it helps !

Create new directory in Julia for each run

I'm running Julia code which generates a plot and a text file. There exists an "Output" folder in the same folder where the code in question is located. For the first run, I create a "Run_1" folder, with "Plots" and "Data" subfolders:
fig_path = #__DIR__
mkdir(fig_path*"/Output/Run_1/")
mkdir(fig_path*"/Output/Run_1/Plots/")
mkdir(fig_path*"/Output/Run_1/Data/")
After plotting, I save the figure to "Plots":
fig_name = "test_figure"
savefig(fig_path*"/Output/Run_1/Plots/"*fig_name*".pdf")
and the output file (contained within "output_matrix") is saved to "Data":
outfile_1 = fig_path*"/Output/Run_1/Data/test_data.txt"
open(outfile_1, "w") do f1
writedlm(f1,output_matrix)
end
However, I want to run this code multiple times. Each time it runs, it should create a new "Run" folder in the "Output" folder, i.e. on the first run its Run_1, the second run it's Run_2, and so on. All folders from previous runs are NOT deleted. In each Run folder, there's a "Plots" and a "Data" folder, and I save the plot and data to their respective folders in each run. How can I have Julia update the file name in such a manner?
The ispath function checks whether a file or directory exists in the filesystem.
If you want to keep a naming convention like Run_1...Run_N, something like this could help:
function mk_output_dir()
i = 1
while true
dir_name = joinpath(#__DIR__, "Output", "run_$i")
if !ispath(dir_name)
mkpath(dir_name)
return dir_name
end
i += 1
end
end
This produces:
# First run
julia> top_dir = mk_output_dir()
"/tmp/Output/run_1"
julia> mkdir(joinpath(top_dir, "Plots"))
"/tmp/Output/run_1/Plots"
julia> mkdir(joinpath(top_dir, "Data"))
"/tmp/Output/run_1/Data"
# Second run
julia> top_dir = mk_output_dir()
"/tmp/Output/run_2"
julia> mkdir(joinpath(top_dir, "Plots"))
"/tmp/Output/run_2/Plots"
julia> mkdir(joinpath(top_dir, "Data"))
"/tmp/Output/run_2/Data"
Be aware that race conditions could occur if you start two instances of your program at the same time.
Alternatively, I personally tend to use naming conventions involving timestamps when creating directory structures like this. Here would be a minimal example:
using Dates
function mk_output_dir()
timestamp = Dates.format(now(), "YYYYmmdd-HHMMSS")
dir_name = joinpath(#__DIR__, "Output", "run_$timestamp")
#assert !ispath(dir_name) "Somebody else already created the directory"
mkpath(dir_name)
return dir_name
end
which produces something like this:
julia> top_dir = mk_output_dir()
"/tmp/Output/run_20201229-210835"
julia> mkdir(joinpath(top_dir, "Plots"))
"/tmp/Output/run_20201229-210835/Plots"
julia> mkdir(joinpath(top_dir, "Data"))
"/tmp/Output/run_20201229-210835/Data"
Maybe something like this:
function mkresultdir(fig_path)
for i=1:1000
rundir = joinpath(fig_path, "run_$i")
if !isdir(rundir)
mkdir(rundir)
return rundir
end
end
error("too many results on disk, time for a cleanup!")
end
res_dir_1 = mkresultdir("/home/my_user/results")
res_dir_2 = mkresultdir("/home/my_user/results")

Workaround for case-sensitive input to dir

I am using Octave 5.1.0 on Windows 10 (x64). I am parsing a series of directories looking for an Excel spreadsheet in each directory with "logbook" in its filename. The problem is these files are created by hand and the filenaming isn't consistent: sometimes it's "LogBook", other times it's "logbook", etc...
It looks like the string passed as input to the dir function is case-sensitive so if I don't have the correct case, dir returns an empty struct. Currently, I am using the following workaround, but I wondered if there was a better way of doing this (for a start I haven't captured all possible upper/lower case combinations):
logbook = dir('*LogBook.xls*');
if isempty(logbook)
logbook = dir('*logbook.xls*');
if isempty(logbook)
logbook = dir('*Logbook.xls*');
if isempty(logbook)
logbook = dir('*logBook.xls*');
if isempty(logbook)
error(['Could not find logbook spreadsheet in ' dir_name '.'])
end
end
end
end
You need to get the list of filenames (either via readdir, dir, ls), and then search for the string in that list. If you use readdir, it can be done like this:
[files, err, msg] = readdir ('.'); # read current directory
if (err != 0)
error ("failed to readdir (error code %d): %s", msg);
endif
logbook_indices = find (cellfun (#any, regexpi (files, 'logbook'));
logbook_filenames = files(logbook_indices);
A much less standard approach could be:
glob ('*[lL][oO][gG][bB][oO][kK]*')

How can i fetch starting of file name from the path with different extensions using R

"/D/data_DataAnalysis/Progrm/datset1/set2/genus/Huttenhower_LC8_genus_reported.tsv"
"/c/bioinfoTools/data/mock/test/truth/file_sets/genus/Huttenhower_LC8_TRUTH.txt"
I want "Huttenhower_LC8" from two file name using R.
Similer to the python code
fileName_temp = a_file.split("/")[-1]
filename = a_file.split("/")[-1][:-9]
for another_file in all_slim_files:
a_filename = another_file.split("/")[-1][:-18]

Resources