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
Related
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'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.
i am using in my unity project this method that saves the camera viewport to the file system:
Application.Capturescreenshot(fileName)
it is work great but i want it to be saved under a specific path.
for example : Application.Capturescreenshot(c:/screenshots/filename);
how can i managed this?
thnaks!
You could use the absolute file path easily by passing it as a string. In your case, using this statement should work just fine:
Application.CaptureScreenshot("C:/screenshots/filename");
Note that it will give you an error if screeshots folder does not exist. Hence, if you are uncertain of that, you could modify the code accordingly:
// File path
string folderPath = "C:/screenshots/";
string fileName = "filename";
// Create the folder beforehand if not exists
if(!System.IO.Directory.Exists(folderPath))
System.IO.Directory.CreateDirectory(folderPath);
// Capture and store the screenshot
Application.CaptureScreenshot(folderPath + fileName);
Lastly, if you want to use a relative path instead of an absolute one, Unity provides dataPath and persistentDataPath variables to access the data folder path of the project. So, if you want to store the screenshots inside the data folder, you could change the folderPath above accordingly:
string folderPath = Application.dataPath + "/screenshots/";
var nameScreenshot = "Screenshot_" + DateTime.Now.ToString("dd-mm-yyyy-hh-mm-ss") + ".png";
ScreenCapture.CaptureScreenshot(nameScreenshot);
Try this !
My image upload doesn't work:
Controller:
if (Input::hasFile('image')) {
$bikecreate->image = Input::file('image');
$destinationPath = public_path().'/upload/';
$filename = str_random(6) . '_' . $bikecreate->users_id ;
Input::file('image')->move($destinationPath, $filename);
}
Form:
{{ Form::file('image', array('files' => true)) }}
After accepting form everything looks ok, but after the end of upload, filepath in database show .tmp/file at my server.
Without seeing the rest of your code it's hard to see exactly what's going on but my guess is that your line $bikecreate->image = Input::file('image') is where you're setting the file's path for the database. You've actually set the UploadedFile instance as the image property on $bikecreate there, which, presumably, when serialised to something to put into the database gets __toString() called on it.
__toString() called on a File instance (which itself inherits __toString from SPLFileInfo returns the path to that file. So you'd think you're get the uploaded filename, but actually because an uploaded file is actually a temporary file in PHP, you get the temporary name.
Try changing that line to the following:
$bikecreate->image = Input::file('image')->getClientOriginalName();
This retrieves the actual original name of the uploaded file, not the temporary path given to it by PHP.
It goes without saying that this is only pertinent to UploadedFiles, normal files should just be able to be __toStringed to get the path to the file, although you'll notice that it would be the full path and not the basename. To get that, use getBaseName().
I would like to be able to run a function from the directory where it is defined.
Let's say this is my folder structure:
./matlab
./matlab/functions1
./matlab/functions2
and I have all directories in my MATLAB path, so I am able to call the functions that are in these directories.
Let's say my function "func" resides in 'matlab/functions1'. My function contains command
csvwrite('data.csv', data(:));
Now, if I call "func" from ./matlab, 'data.csv' gets created in ./matlab. If I call it from ./matlab/functions2, it will get created in that directory. But I would like for the function to write 'data.csv' always in the directory, where the function is defined (./matlab/functions1), no matter what my current directory is. How can I achieve that?
mfilename called from 'inside' a function returns the function path and name.
fullPath = mfilename('fullpath');
pathString = fileparts(fullPath);
dataPath = [ pathString filesep 'data.csv'];
csvwrite(dataPath, data(:));
In addition to what #zellus suggested, you can use functions to get information on a specific function, regardless of any m file being executed at the same moment. You set the function of interest by giving functions the function handle:
funInfo = functions(#func);
fullPath = funInfo.file;