Matlab Load from relative path - matlab

function []= read_c3d_feat(output_list_relative)
dir_list = importdata(output_list_relative);
dim_feat = 512;
for i = 1 : size(dir_list, 1)
dir_str = char(dir_list(i));
feat_files = dir([dir_str, '/*.res5b']);
num_feat = length(feat_files);
feat = zeros(num_feat, dim_feat);
for j = 1 : num_feat
feat_path = strcat(dir_str, '/', feat_files(j).name);
...............
....................so on
Give me error like
Error using dir
Invalid path. The path must not contain a null character.
Error in read_c3d_feat (line 12)
feat_files = dir([dir_str, '/*.res5b']);

Your dir_list variable must have strings which contain null characters, as the error tells you. If you try using hard-coded strings you will see it works:
function read_c3d_feat(output_list_relative)
dir_list = {'21';'45';'18'};
for i = 1:size(dir_list, 1)
dir_str = dir_list{i}; % Loops through '21','45','18'
% The dir function now works because we know dir_str is a valid string
feat_files = dir([dir_str, '/*.res5b']);
end
end
This means you need to debug your code and find out what this line is actually assigning to dir_list:
dir_list = importdata(output_list_relative);
Note that if dir_list is a cell of text entries, you should be indexing it with curly braces as above. If instead it is a matrix (because all of the entries seem to be numerical anyway) then you should be using num2str when passing to dir:
function read_c3d_feat(output_list_relative)
dir_list = importdata(output_list_relative);
dim_feat = 512;
for i = 1:size(dir_list, 1)
feat_files = dir([num2str(dir_list(i)), '/*.res5b']);
% ...

Related

Unable to read file 'subjFiles(1).name'. No such file or directory

I'm using some codes that called specific function.
in line 17 of this function I get the error
Index exceeds matrix dimensions.
Error in generateExpReport (line 17)
checkPValuesField = subjFiles(1).name;
the function up to line 20 is like this:
function [] = generateExpReport(copyDir,resultDir,params)
% Syntax
methodNames = fieldnames(params.methods);
numMethods = length(methodNames);
for i = 1 : numMethods
cd(resultDir)
numTargets = length(params.methods.(methodNames{i,1}).idTargets);
idDrivers = params.methods.(methodNames{i,1}).idDrivers;
nameFiles = [methodNames{i,1} '*.mat'];
subjFiles = dir(nameFiles);
numSubj = length(subjFiles);
significanceOnDrivers = zeros(numSubj,numTargets);
matrixTransferEntropy = zeros(numSubj,(numTargets)+1);
% check if the pValues matrix is present
checkPValuesField = load(subjFiles(1).name);
fields = fieldnames(checkPValuesField);
nameFields = checkPValuesField.(fields{1,1});
I can't find the problem
please help me:(( what's wrong with
checkPValuesField = load(subjFiles(1).name);

How to import in Matlab a cellarray from a .txt file with columns as "text"?

I have a .txt file which I want to import with code in Matlab as a cell. This .txt file has lines with different lengths and a combination of numbers and characters in them. Here is a sample of the file:
*** After labeling ***
Elements: E11, E21, E31, E51, E61, E81,
Connections: E11E61, E21E81, E31E61, E31E81, E51E81,
*** After labeling ***
Elements: E11, E21, E31, E51, E61, E81,
Connections: E11E61, E21E81, E31E51, E31E81, E61E81,
*** After labeling ***
Elements: E11, E21, E31, E51, E61, E62, E81,
Connections: E11E61, E21E81, E31E51, E31E62, E61E81, E62E81,
The results should look like this:
When I import the file using the Import Tool some columns are recognized as text and some as numbers. I have to manually change the type of the column from number to text for each of them:
.
More, I have to select cellarray instead of Column Vectors each time. The maximum length of the lines is known, 15.
I tried results = importfile('results') but it does not work. Does anybody have any suggestions for me?
Here is some simple parsing routine that returns directly elements and connections as cell vectors.
NB: Parsing assumes text file is well formatted and is always elements, followed by connections.
function [elements, connections] = ReadElementsAndConnections(filename)
%[
% For debug
if (nargin < 1), filename = 'ElementsAndConnections.txt'; end
% Read full file content
text = fileread(filename);
% Split on newline
lines = strsplit(strtrim(text), '\n');
% Parse
elements = cell(0,1);
connections = cell(0,1);
isElementLine = true;
lcount = length(lines);
startElements = length('Elements:') + 1;
startConnections = length('Connections:') + 1;
for li = 1:lcount,
% Skip empty lines or lines starting with '*'
line = strtrim(lines{li});
if (isempty(line) || (line(1) == '*')), continue; end
% NOT VERY SAFE: Assuming we always have 'elements', followed by 'connections'
if (isElementLine)
elementsLine = lines{li}(startElements:end);
elementsValues = strtrim(strsplit(elementsLine, ','));
elements{end+1} = elementsValues(1:(end-1)); % Last value is empty.
isElementLine = false;
else
connectionsLine = lines{li}(startConnections:end);
connectionsValues = strtrim(strsplit(connectionsLine, ','));
connections{end+1} = connectionsValues(1:(end-1)); % Last value is empty.
isElementLine = true;
end
end
%]
end
You can then access elements and connections like this:
>> elements{1}
ans =
'E11' 'E21' 'E31' 'E51' 'E61' 'E81'
>> connections{3}
ans =
'E11E61' 'E21E81' 'E31E51' 'E31E62' 'E61E81' 'E62E81'

Error while trying to open files in Matlab

My code has 2 parts. First part is an automatic file opening programmed like this :
fichierref = 'H:\MATLAB\Archive_08112012';
files = dir(fullfile(fichierref, '*.txt'));
numberOfFiles = numel(files);
delimiterIn = ' ';
headerlinesIn = 11;
for d = 1:numberOfFiles
filenames(d) = cellstr(files(d).name);
end
for i=1:numberOfFiles
data = importdata(fullfile(fichierref,filenames{i}),delimiterIn,headerlinesIn);
end
Later on, I want the user to select his files for analysis. There's a problem with this though. I typed the lines as follow :
reference = warndlg('Choose the files from which you want to know the magnetic field');
uiwait(reference);
filenames = cellstr(uigetfile('./*.txt','MultiSelect', 'on'));
numberOfFiles = numel(filenames);
delimiterIn = ' ';
headerlinesIn = 11;
It's giving me the following error, after I press OK on the prompt:
Error using cellstr (line 34)
Input must be a string.
Error in FreqVSChampB_no_spec (line 128)
filenames = cellstr(uigetfile('./*.txt','MultiSelect', 'on'));
Anyone has an idea why it's doing that?
You do not need the cellstr command for the output of uigetfile in 'MultiSelect' mode: the output is already in a cellarray form (see doc of uigetfile).

Matlab function calling basic

I'm new to Matlab and now learning the basic grammar.
I've written the file GetBin.m:
function res = GetBin(num_bin, bin, val)
if val >= bin(num_bin - 1)
res = num_bin;
else
for i = (num_bin - 1) : 1
if val < bin(i)
res = i;
end
end
end
and I call it with:
num_bin = 5;
bin = [48.4,96.8,145.2,193.6]; % bin stands for the intermediate borders, so there are 5 bins
fea_val = GetBin(num_bin,bin,fea(1,1)) % fea is a pre-defined 280x4096 matrix
It returns error:
Error in GetBin (line 2)
if val >= bin(num_bin - 1)
Output argument "res" (and maybe others) not assigned during call to
"/Users/mac/Documents/MATLAB/GetBin.m>GetBin".
Could anybody tell me what's wrong here? Thanks.
You need to ensure that every possible path through your code assigns a value to res.
In your case, it looks like that's not the case, because you have a loop:
for i = (num_bins-1) : 1
...
end
That loop will never iterate (so it will never assign a value to res). You need to explicitly specify that it's a decrementing loop:
for i = (num_bins-1) : -1 : 1
...
end
For more info, see the documentation on the colon operator.
for i = (num_bin - 1) : -1 : 1

Input argument "ip" is undefined Error

I am new in matlab I am trying to define a function and I keep getting this error
" input argument "ip" is undefined.
Error in ==> edge_mapping at 2
size_ip = size(ip(:,:,1)); "
here is my code
function[op1,op2,op3] = edge_mapping(ip)
size_ip = size(ip(:,:,1));
s=size_ip(1);
op1= cat(3,zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s));
op2= cat(3,zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s));
op3= cat(3,zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s));
for i = 1 : 10
op1(:,:,i)=edge(ip(:,:,i),'sobel');
op2(:,:,i)=edge(ip(:,:,i),'prewitt');
op3(:,:,i)=edge(ip(:,:,i),'canny');
end
function [op1, op2, op3] = edge_mapping(ip)
op1 = zeros(size(ip));
op2 = zeros(size(ip));
op3 = zeros(size(ip));
for i = 1 : size(ip, 3)
op1(:,:,i)=edge(ip(:,:,i),'sobel');
op2(:,:,i)=edge(ip(:,:,i),'prewitt');
op3(:,:,i)=edge(ip(:,:,i),'canny');
end
return
But I think it's better to write a simple function:
function op = edge_mapping(ip, edge_mode)
op = zeros(size(ip));
for i = 1 : size(ip, 3)
op(:,:,i)=edge(ip(:,:,i), edge_mode);
end
return
and then call it:
op_sobel = edge_mapping(ip, 'sobel');
op_prewitt = edge_mapping(ip, 'prewitt');
op_canny = edge_mapping(ip, 'canny');
This code is a function. It has to be save as m-file and run from MATLAB command line, script or another function as
[op1,op2,op3] = edge_mapping(ip);
where arguments ip, op1, op2, and op3 can have different names.
Make sure you have space after function keyword.
To avoid this error you can assign a default value for the input argument if it's undefined (not exist in the function's scope):
if ~exist(ip, 'var')
ip = []; %# or other default value
end