I'm pretty new to Matlab, and I'm looking for possibly a way to open a file call data.txt from several subfolders of 2414A,2443A,6732A,4577A... and so on, without overwriting on top of each other. All of them are in one giant folder, just within different subfolders.
My question is, instead of changing the folder name every time I open the data.txtand setting a variable for each of the txt file, is there a quicker way to do so? Because my end goal is to concatenate all of the data.txt matrcies for computation.
I currently just have:
cd C:\User\Aisk_000\Desktop\A\NC\Subjects\2414A\
NC1 = dlmread('data.txt');
cd ../2443A\
NC2 = dlmread('data.xt');
cd ../6732A\
...etc. It definitely serves the job, though.
As simple as this:
files = dir('C:\User\Aisk_000\Desktop\A\NC\Subjects\*\data.txt');
files_num = numel(files);
files_data = cell(files_num,1);
for i = 1:files_num
file = files(i);
file_path = fullfile(file.folder,file.name);
files_data{i} = dlmread(file_path);
end
If you want to build up a simple indexing system, use this code instead:
files = dir('C:\Users\Zarathos\Desktop\*\data.txt');
files_num = numel(files);
files_data = cell(files_num,2);
for i = 1:files_num
file = files(i);
file_folder_idx = strsplit(file.folder,'\');
file_folder_idx = file_folder_idx{end};
file_path = fullfile(file.folder,file.name);
files_data{i,1} = file_folder_idx;
files_data{i,2} = dlmread(file_path);
end
So if you have to save your files back to disk after they have been modified, you will be able to rebuild the structure of your C:\User\Aisk_000\Desktop\A\NC\Subjects\ folder and know in which path you have to save the file data currently being processed.
Related
I don't know why when I generate files fsa.generateFile(fileName, finalString) it creates the files fine, but when I clean the project, it doubles the output.
Even if I delete the file, it continues growing.
Is this a code or Eclipse problem?
Thank you.
you store the file content for some reason as a member in the generator and never reset it
val root = resource?.allContents?.head as ProblemSpecification;
s += readFile(path_sigAlloyDeclaration+"sigAlloyDeclaration.txt")
i assume s either should be local to the doGenerate method or be reset at the start
s = ""
val root = resource?.allContents?.head as ProblemSpecification;
s += readFile(path_sigAlloyDeclaration+"sigAlloyDeclaration.txt")
I have a GUI using a Browe Button to search a file :
function Browse(app, event)
FileName,FilePath ]= uigetfile();
ExPath = fullfile(FilePath, FileName);
app.FileTextArea.Value = ExPath;
end
And i save the file Path in a Text Area.
I have another button that start a matlab script with the file path as parameter and so i would like to accept only a certain type of file (.ctm which is my own type of file) if possible like this :
if file is .ctm
do something
else
print('a .ctm file is needed')
Thanks for helping
There are two things you can do:
Display only the files with a certain extension with uigetfile()
[fileName, dataDir] = uigetfile('*.ctm', 'Select a *.ctm file', yourDefaultPth);
Verify that selected file has a .ctm extension
[data.dir,data.fileName,data.ext] = fileparts(fullfile(dataDir, fileName)); % dataDir and fileName from pt. 1
if strcmp(data.ext, '.ctm')
% do something
else
print('a .ctm file is needed')
end
Keep in mind that neither of the two will verify that the content of the file is the one you're expecting and if someone will manually modify extension of the file, your program will most likely crash. It's good for a start but if you want to do a more reliable check, you should verify that the content of the file is correct, not its extension.
I used imresize to resize about 20images. I want the resized images to be stored in a new folder I created. Here is the code I used:
imwrite(myoutput, 'resized.png');
Now my problem us that, I only get one image written to the working directory, named 'resized.PNG'. I want all the 20 resized images to be saved.
I also want them saved in a new folder defined by mkdir(resizedFolder)... Which I don't know how to do.
Here is an excerpt from my codes:
dirD=dir('*.jpeg');
for k=1:length(dirD); %k=20 %technically
%i ran a long code to find a %rectangular boundary, and cropped.
CropIm=imcrop(I, thisBlobsBoundingBox);
resizedIm=imresize(CropIm, 0.1);
end
Now, I want resizedIm to be stored in resizedFolder as individual images which should give me 20 images.
You're going to want to use fullfile to combine the directory and filenames. Also you will want to create a custom filename for each image. Below I assume that all of your images are in a cell array.
resizedFolder = '/path/to/folder';
% Create folder if it doesn't exist
if ~exist(resizedFolder, 'dir')
mkdir(resizedFolder);
end
dirD = dir('*.jpeg');
for k = 1:numel(dirD);
% Your code to get the boundary
CropIm = imcrop(I, thisBlobsBoundingBox);
resizedIm = imresize(CropIm, 0.1);
% Create a custom filename for this image.
filename = sprintf('resized%02d.png', k);
imwrite(resizedIm, fullfile(resizedFolder, filename));
end
This will create images in the folder that you specify with the filenames being: resized01.png, resized02.png, ...
Update: I have updated my response to be more specific to your initial code.
I would store your current directory to a variable like
curPath = cd;
newPath = newDirectory;
cd(curPath);
cd newPath;
After this, your working directory is the new folder. Run the loop to save your files.
Make sure you are iterating the file names.
for i = 1:20
imwrite(myoutput(i), ['resized', num2str(i),'.png']);
end
I'm running a code in Maltab the creates directories through mkdir. Problem is, I'm creating their name by some logic on run-time, so I don't know what the dir name would be. I know I can first create the name as
string dirName = nameLogic();
mkdir(dirName);
but I would like to know the dirName from the created directory itself. Naivly, that would be
[outputdirName] = mkdir(fuzzylogicdirName);
I should add that I'm not religiously attached to mkdir, and another yet more suitable method might be in place.
Thanks
I might get you wrong. In any case what mkdir does is just creating a folder, hence the folder name must be known (possibly determined at run-time) before the call.
A structure like
folderName = folderNameLogic([run_time_variables]);
% # folderName = 'something_run_time_variables(1)_and_run_time_variables(2)'
status = mkdir(folderName)
if status == 1
disp(['success in creating folder ' folderName]);
else
disp(['ERROR in creating folder ' folderName]);
end
is thus necessary.
Clearly nothing prevents you from wrapping the call a function of yours returning the folder name. E.g.
function [folderName] = mkdir_retname(folderName)
status = mkdir(folderName);
if status == 0
folderName = '0';
end
end
All I want is to save my user(player) highscores, and this information to persist between application(game) launches in Corona SDK (Lua). I do want it to work on iOS and Android nicely. My highscores data is actually two lua tables containing numbers.
What's the correct and easiest way to do it?
You may save the scores into a table, and then serialize it into json format text file.
local json=require("json")
local savefile="scores.json"
scores=
{
{
level=1,
status=0,
highscore=0,
},
{
level=2,
status=0,
highscore=0,
},
}
function getScore(filename, base)
-- set default base dir if none specified
if not base then
base = system.DocumentsDirectory
end
-- create a file path for corona i/o
local path = system.pathForFile(filename, base)
-- will hold contents of file
local contents
-- io.open opens a file at path. returns nil if no file found
local file = io.open(path, "r")
local scores
if file then
-- read all contents of file into a string
contents = file:read( "*a" )
if content ~= nil then
scores=json.decode(content)
end
io.close(file) -- close the file after using it
end
return scores
end
function saveScore(filename, base)
-- set default base dir if none specified
if not base then
base = system.DocumentsDirectory
end
-- create a file path for corona i/o
local path = system.pathForFile(filename, base)
-- io.open opens a file at path. returns nil if no file found
local file = io.open(path, "wb")
if file then
-- write all contents of file into a string
file:write(json.encode(scores))
io.close(file) -- close the file after using it
end
end
The global scores variable can be manipulated like a normal table, and when you want to load or save the scores table you can call the functions above.