import data and Looping for the file - matlab

My purpose is to split the string into part then check whether the 'f11_data' contain those split word. if yes then return 0, if no then return 1. I have 100 strings, but it doesn't make sense to type the str/needles 100 inside my code times. How do I use looping to do that? I'm facing a problem using importdata.
str1 = 'http://en.wikipedia.org/wiki/hostname';
str2 = 'http://hello/world/hello';
str3 = 'http://hello/asd/wee';
f11_data1 = 'hostname From wikipedia, the free encyclopedia Jump to: navigation, search In computer networking, a hostname (archaically nodename .....';
f11_data2 = 'hell';
f11_data3 = 'hello .....';
needles1 = strcat('\<', regexpi(str1,'[:/.]*','split'), '\>')
needles2 = strcat('\<', regexpi(str2,'[:/.]*','split'), '\>')
needles3 = strcat('\<', regexpi(str3,'[:/.]*','split'), '\>')
~cellfun('isempty', regexpi(f11_data1, needles1, 'once'))
~cellfun('isempty', regexpi(f11_data2, needles2, 'once'))
~cellfun('isempty', regexpi(f11_data3, needles3, 'once'))
This is how I modified the above code using a loop:
data = importdata('URL')
needles = regexp(data,'[:/.]*','split') %// note the different search string
for i = 1:2
A11_data = needles{i};
data2 = importdata(strcat('f11_data', int2str(i)));
%feature11_data=(~cellfun('isempty', regexpi(data2, needles, 'once')))
%feature11(i)=feature11_data
~cellfun('isempty', regexpi(data2, needles, 'once'))
end
I get the error :
"
??? Error using ==> regexpi
All cells for regexpi must be strings.
Error in ==> f11_test2 at 14
~cellfun('isempty', regexpi(haystack, needles, 'once')) "

It's hard to tell what you are trying to do but, if you want to do something equivalent,
for i = 1:3
str = importdata(URL_LIST[i])
needle = strcat('\<', regexp(str,'[:/.]*','split'), '\>');
haystack = importdata(HAYSTACK_LIST[i]);
~cellfun('isempty', regexpi(haystack, needle, 'once'))
end
Obviously you need to define URL_LIST and HAYSTACK_LIST.

Related

how to read broken numbers on two lines in a text file in matlab?

how to read broken numbers on two lines in matlab?
I am generating some results in text files that are being broken into two lines. Example:
text x = 1.
2345 text
What would a code look like to read the value x = 1.2345?
Suppose the value of x = 1.2345 is in file named name.txt.
When it doesn't break the values I'm looking for:
text x = 1.2345 text
I use the following (working) code:
buffer = fileread('name.txt') ;
search = 'x = ' ;
local = strfind(buffer, search);
xvalue = sscanf(buffer(local(1,1)+numel(search):end), '%f', 1);
You can remove line breaks (and other "white space", if needed) before parsing the string:
>> str = sprintf('text x = 1.\n2345 text')
str =
'text x = 1.
2345 text'
>> str = regexprep(str, '\n', '')
str =
'text x = 1.2345 text'

Matlab Load from relative path

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']);
% ...

Excel Range object throws error 0x800A03EC because of a string longer than 255 characters

Using an ActiveX server from MATLAB, I am trying to highlight many cells in an Excel sheet at once. These are not in specific columns or rows so I use Range('A1,B2,...') to access them. However the string accepted by the Range object has to be less than 255 characters or an error:
Error: Object returned error code: 0x800A03EC
is thrown. The following code reproduces this error with an empty Excel file.
hActX = actxserver('Excel.Application');
hWB = hActX.Workbooks.Open('C:\Book1.xlsx');
hSheet = hWB.Worksheets.Item('Sheet1');
col = repmat('A', 100, 1);
row = num2str((1:100)'); %'
cellInd = strcat(col, strtrim(cellstr(row)));
str1 = strjoin(cellInd(1:66), ','); %// 254 characters
str2 = strjoin(cellInd(1:67), ','); %// 258 characters
hSheet.Range(str1).Interior.Color = 255; %// Works
hSheet.Range(str2).Interior.Color = 255; %// Error 0x800A03EC
hWB.Save;
hWB.Close(false);
hActX.Quit;
How can I get around this? I found no other relevant method of calling Range, or of otherwise getting the cells I want to modify.
If you start with a String, you can test its length to determine if Range() can handle it. Here is an example of building a diagonal range:
Sub DiagonalRange()
Dim BigString As String, BigRange As Range
Dim i As Long, HowMany As Long, Ln As String
HowMany = 100
For i = 1 To HowMany
BigString = BigString & "," & Cells(i, i).Address(0, 0)
Next i
BigString = Mid(BigString, 2)
Ln = Len(BigString)
MsgBox Ln
If Ln < 250 Then
Set BigRange = Range(BigString)
Else
Set BigRange = Nothing
arr = Split(BigString, ",")
For Each a In arr
If BigRange Is Nothing Then
Set BigRange = Range(a)
Else
Set BigRange = Union(BigRange, Range(a))
End If
Next a
End If
BigRange.Select
End Sub
For i = 10, the code will the the direct method, but if the code were i=100, the array method would be used.
The solution, as Rory pointed out, is to use the Union method. To minimize the number of calls from MATLAB to the ActiveX server, this is what I did:
str = strjoin(cellInd, ',');
isep = find(str == ',');
isplit = diff(mod(isep, 250)) < 0;
isplit = [isep(isplit) (length(str) + 1)];
hRange = hSheet.Range(str(1:(isplit(1) - 1)));
for ii = 2:numel(isplit)
hRange = hActX.Union(hRange, ...
hSheet.Range(str((isplit(ii-1) + 1):(isplit(ii) - 1))));
end
I used 250 in the mod to account for the cell names being up to 6 characters long, which is sufficient for me.

Concatenating text from different text files into one

Suppose there are five text files. Contents of files are textfile1 = i saw an alligator, textfile2 = alligator was sitting near a tree, textfile3 = alligator was sleeping, textfile4 = parrot was flying, textfile5 = parrot was flying.
I used the code below to find paths of textfiles containing word alligator:
sdirectory = 'C:\Users\anurag\Desktop\Animals\Annotations\';
textfiles = dir([sdirectory '*.eng']);
num_of_files = length(textfiles);
C = cell(num_of_files, 1);
for w = 1:length(textfiles)
file = [sdirectory textfiles(w).name];
%// load string from a file
STR = importdata(file);
BL = cellfun(#lower, STR, 'uni',0);
%// extract string between tags
%// assuming you want to remove the angle brackets
B = regexprep(BL, '<.*?>','');
B(strcmp(B, '')) = [];
%// split each string by delimiters and add to C
tmp = regexp(B, '/| ', 'split');
C{w} = [tmp{:}];
end
where = [];
for j = 1:length(C)
file1 = [sdirectory textfiles(j).name];
if find(ismember(C{j},'alligator'))
where = [where num2str(j) '.eng, '];
disp(file1)
end
end
Finally the variable file1 will show the path of textfile containing word alligator one by one. Is there any way to concatenate the strings of textfiles containing the required word into a cell array.

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).