how to dlmwrite a file from array - matlab

How to write the cell as below in text file(my_data.out)?
http_only = cell2mat(http_only)
dlmwrite('my_data.out',http_only)
I get the error as below:(I have tried to solve but still return me the error)
Here is my full code:
I want to generate the text file for each of the data which only store 'http_only'
then check for that is it meet the word in split_URL.
%data = importdata('DATA/URL/training_URL')
data = importdata('DATA/URL/testing_URL')
domain_URL = regexp(data,'\w*://[^/]*','match','once')
no_http_URL = regexp(domain_URL,'https?://(?:www\.)?(.*)','tokens','once');
no_http_URL = vertcat(no_http_URL{:});
split_URL = regexp(no_http_URL,'[:/.]*','split')
[sizeData b] = size(split_URL);
for i = 1:100
A7_data = split_URL{i};
data2=fopen(strcat('DATA\WEBPAGE_SOURCE\TESTING_DATA\',int2str(i),'.htm'),'r')
CharData = fread(data2, '*char')'; %read text file and store data in CharData
fclose(data2);
img_only = regexp(CharData, '<img src.*?>', 'match'); %checking
http_only = regexp(img_only, '"http.*?"', 'match');
http_only1 = horzcat(http_only{:});
fid = fopen('my_data.out',int2str(i),'w');
for col = 1:numel(http_only1)
fprintf(fid,'%s\n',http_only1{:,col});
end
fclose(fid);
feature7_data=(~cellfun('isempty', regexpi(CharData , A7_data, 'once')))
B7(i)=sum(feature7_data)
end
feature7(B7>=5)=-1;
feature7(B7<5&B7>2)=0;
feature7(B7<=2)=1;
feature7'

Write cell-by-cell using fprintf -
fid = fopen('my_data.out','w');
for col = 1:numel(http_only)
fprintf(fid,'%s\n',http_only{:,col});
end
fclose(fid);
Edit 1: If your input is a cell array of cell arrays, use this code instead.
Code
http_only1 = horzcat(http_only{:});
fid = fopen('my_data.out','w');
for col = 1:numel(http_only1)
fprintf(fid,'%s\n',http_only1{:,col});
end
fclose(fid);
Edit 2: For a number of inputs to be stored into separate files, use this demo -
data1 = {{'[]'} {'"http://google.com"'} {'"http://yahoo.com'}};
data2 = {{'[]'} {'"http://overflow.com"'} {'"http://meta.exchange.com'}};
data = cat(1,data1,data2);
for k = 1:size(data,1)
data_mat = horzcat(data{k,:});
out_filename = strcat(out_basename,num2str(k),'.out');
fid = fopen(out_filename,'w');
for col = 1:numel(data_mat)
fprintf(fid,'%s\n',data_mat{:,col});
end
fclose(fid);
end

Related

printing the content of a cell in matlab into a file

I have a cell datastructure and I am trying to print it to a file I keep getting this error :
Error using fprintf
Function is not defined for 'cell' inputs.
solWT = optimizeCbModel(recon1,'max','one');
fileID = fopen('Q3.txt','w');
grRateWT = solWT.f
C=cell(2,2);
for system = 1:2%length(recon1.subSystems)
system
fluxReactions = recon1.rxns(ismember(recon1.subSystems,recon1.subSystems(system)));
for reaction = 1:length(fluxReactions)
model = recon1;
model.lb(reaction) = 0;
model.ub(reaction) = 0;
end
solKO = optimizeCbModel(model,'max');
C{system, 1} = recon1.subSystems(system);
C{system, 2} = solKO.f / grRateWT;
end
sortedCellArray = sortrows(C,2);
formatSpec = '%s %2.1f\n';
[nrows,ncols] = size(C);
for row = 1:nrows
fprintf(fileID,formatSpec,sortedCellArray{row,:});
end
What am I doing wrong
Try
fprintf(fileID,formatSpec,sortedCellArray{row,1}, sortedCellArray{row,2})

Importing data block with Matlab

I have a set of data in the following format, and I would like to import each block in order to analyze them with Matlab.
Emax=0.5/real
----------------------------------------------------------------------
4.9750557 14535
4.9825821 14522
4.990109 14511
4.9976354 14491
5.0051618 14481
5.0126886 14468
5.020215 14437
5.0277414 14418
5.0352678 14400
5.0427947 14372
5.0503211 14355
5.0578475 14339
5.0653744 14321
Emax=1/real
----------------------------------------------------------------------
24.965595 597544
24.973122 597543
24.980648 597543
24.988174 597542
24.995703 597542
25.003229 597542
I have modified this piece of code from MathWorks, but I think, I have problems dealing with the spaces between each column.
Each block of data consist of 3874 rows and is divided by a text (Emax=XX/real) and a line of ----, unfortunately is the only way the software export the data.
Here is one way to import the data:
% read file as a cell-array of lines
fid = fopen('file.dat', 'rt');
C = textscan(fid, '%s', 'Delimiter','');
C = C{1};
fclose(fid);
% remove separator lines
C(strncmp('---',C,3)) = [];
% location of section headers
headInd = [find(strncmp('Emax=', C, length('Emax='))) ; numel(C)+1];
% extract each section
num = numel(headInd)-1;
blocks = struct('header',cell(num,1), 'data',cell(num,1));
for i=1:num
% section header
blocks(i).header = C{headInd(i)};
% data
X = regexp(C(headInd(i)+1:headInd(i+1)-1), '\s+', 'split');
blocks(i).data = str2double(vertcat(X{:}));
end
The result is a structure array containing the data from each block:
>> blocks
blocks =
2x1 struct array with fields:
header
data
>> blocks(2)
ans =
header: 'Emax=1/real'
data: [6x2 double]
>> blocks(2).data(:,1)
ans =
24.9656
24.9731
24.9806
24.9882
24.9957
25.0032
This should work. I don't think textscan() will work with a file like this because of the breaks between blocks.
Essentially what this code does is loop through lines between blocks until it finds a line that matches the data format. The code is naive and assumes that the file will have exactly the number of blocks lines per block that you specify. If there were a fixed number of lines between blocks it would be a lot easier and you could remove the first inner loop and replace with just ~=fgets(fid) once for each line.
function block_data = readfile(in_file_name)
fid = fopen(in_file_name, 'r');
delimiter = ' ';
line_format = '%f %f';
n_cols = 2; % Number of numbers per line
block_length = 3874; % Number of lines per block
n_blocks = 2; % Total number of blocks in file
tline = fgets(fid);
line_data = cell2mat(textscan(tline,line_format,'delimiter',delimiter,'MultipleDelimsAsOne',1));
block_n = 0;
block_data = zeros(n_blocks,block_length,n_cols);
while ischar(tline) && block_n < n_blocks
block_n = block_n+1;
tline = fgets(fid);
if ischar(tline)
line_data = cell2mat(textscan(tline,line_format,'delimiter',delimiter,'MultipleDelimsAsOne',1));
end
while ischar(tline) && isempty(line_data)
tline = fgets(fid);
line_data = cell2mat(textscan(tline,line_format,'delimiter',delimiter,'MultipleDelimsAsOne',1));
end
line_n = 1;
while line_n <= block_length
block_data(block_n,line_n,:) = cell2mat(textscan(tline,line_format,'delimiter',delimiter,'MultipleDelimsAsOne',1));
tline = fgets(fid);
line_n = line_n+1;
end
end
fclose(fid)

Creat a loop to generate a a mean value of data which I got form TXT files?

I trying to do script to evalute my lab data into matlab, I have lots of txt files for many samples, each sample has 30 txt files. I did a function to get the data from these files and store them into a structure that contains data and labels.
I would like to know if it is possible to load all the 30 files using loop instead of line by line .
function s = try1(fn)
% adapt for other file format according to your needs
fid = fopen(fn);
s.data = [];
% skip first lines
for k=1:6
l = fgetl(fid);
% read header
a = regexp(l,',','split');
s.labels = a;
l = fgetl(fid);
k=0;
end
while( (l~=-1)&(k<130) )
l = l(1:end-1);
a = regexp(l,', ','split');
a = regexpre
p(a, ',','.');
s.data = [s.data; str2double(a(2:4))];
l = fgetl(fid);
k = k+1;
end
fclose(fid);
end
To do what you want with a data directory and processing each file in a loop you can use a combination of fullfile(...) and a loop. This will likely work:
dataDirectory = 'c:/myDirectory';
allFilesDir = dir(fullfile(dataDirectory , '*.txt'));
allFN = {allFilesDir.name}
% Or do this:
% allFN = {'file1.txt', 'file2.txt'};
result = [];
for ind = 1:length(allFN)
myFN = fullfile(dataDirectory, allFN{ind});
result(ind) = try1(myFN);
end
% Now get the mean:
myMean = mean([result.data])

Matlab Muliple delimiter removing during import

I am struggling with a problem and want a easy way around. I have a big array of data where I have some vector values as ( 1.02 1.23 3.32) format. I want it as 1.02 1.23 3.32 in a tabular form. The problem here is that there are two types of delimiter '(' and ')'.
can anyone help in writing a code for this? I have something like this:
filename = 'U.dat';
delimiterIn = '(';
headerlinesIn = 0;
A = textscan(filename,delimiterIn,headerlinesIn);
But one thing is that it only have one delimiter "(" and it does not work either.
Nishant
If your text file looks something like this:
(1 2 3) (4 5 6)
(7 8 9) (10 11 12)
You can read it in as strings and convert it to a cell array of vectors like this:
% read in file
clear all
filename = 'delim.txt';
fid = fopen(filename); %opens file for reading
tline = fgets(fid); %reads in a line
index = 1;
%reads in all lines until the end of the file is reached
while ischar(tline)
data{index} = tline;
tline = fgets(fid);
index = index + 1;
end
fclose(fid); %close file
% convert strings to a cell array of vectors
rowIndex = 1;
colIndex = 1;
outData = {};
innerStr = [];
for aCell = data % for each entry in data
sline = aCell{1,1};
for c = sline % for each charecter in the line
if strcmp(c, '(')
innerStr = [];
elseif strcmp(c, ')')
outData{rowIndex,colIndex} = num2str(innerStr);
colIndex = colIndex + 1;
else
innerStr = [innerStr, c];
end
end
rowIndex = rowIndex + 1;
colIndex = 1;
end
outData

Binary file - Matrix Manipulation

I have several binary files with a known structure (8,12000,real*4). 8 is the number of variables, and 12000 represent the time steps. From these binaries I need to get a final 16x9 matrix defined as followed:
the first column contains a filename identification.
on the diagonal are located the extreme values (maxima and minima) of the corresponding variable.
the simultaneous values of the other variables shall be given in the rows.
At the moment I'm using this code
for i = 1:num_files
for j = 1:num_ext
fid = fopen([fullfile(BEGINPATH{1},FILELIST{i}) '.$' VAREXTENSION{j}],'r');
A = fread(fid,[DLC_dimens{i}{3}(1) DLC_dimens{i}{3}(2)],'real*4');
for k = 1:size(A,1)
[max_val(k,i) max_idx(k,i)] = max(A(k,:));
[min_val(k,i) min_idx(k,i)] = min(A(k,:));
end
fclose(fid);
end
end
% Pre-allocate ULS matrices
uloads = cell(2*size(A,1),size(A,1)+1);
uloads_temp = cell(2*size(A,1),size(A,1)+1);
% Generate ULS matrix for the first file
for i = 1:size(uloads,1)
uloads{i,end} = DLC_dimens{1}(1);
end
fid = fopen([fullfile(BEGINPATH{1},FILELIST{1}) '.$' VAREXTENSION{1}],'r');
A = fread(fid,[DLC_dimens{i}{3}(1) DLC_dimens{i}{3}(2)],'real*4');
for j = 1:size(uloads,2)-1
uloads{1,j} = A(j,max_idx(1,1))*DLC_dimens{1}{4};
uloads{2,j} = A(j,min_idx(1,1))*DLC_dimens{1}{4};
uloads{3,j} = A(j,max_idx(2,1))*DLC_dimens{1}{4};
uloads{4,j} = A(j,min_idx(2,1))*DLC_dimens{1}{4};
uloads{5,j} = A(j,max_idx(3,1))*DLC_dimens{1}{4};
uloads{6,j} = A(j,min_idx(3,1))*DLC_dimens{1}{4};
uloads{7,j} = A(j,max_idx(4,1))*DLC_dimens{1}{4};
uloads{8,j} = A(j,min_idx(4,1))*DLC_dimens{1}{4};
uloads{9,j} = A(j,max_idx(5,1))*DLC_dimens{1}{4};
uloads{10,j} = A(j,min_idx(5,1))*DLC_dimens{1}{4};
uloads{11,j} = A(j,max_idx(6,1))*DLC_dimens{1}{4};
uloads{12,j} = A(j,min_idx(6,1))*DLC_dimens{1}{4};
uloads{13,j} = A(j,max_idx(7,1))*DLC_dimens{1}{4};
uloads{14,j} = A(j,min_idx(7,1))*DLC_dimens{1}{4};
uloads{15,j} = A(j,max_idx(8,1))*DLC_dimens{1}{4};
uloads{16,j} = A(j,min_idx(8,1))*DLC_dimens{1}{4};
end
fclose(fid);
% ULS temporary matrix generation
uls = uloads;
for i = 2:num_files
fid = fopen([fullfile(BEGINPATH{1},FILELIST{i}) '.$' VAREXTENSION{1}],'r');
A = fread(fid,[8 12000],'float32');
for j = 1:size(uloads,1)
uloads_temp{j,9} = DLC_dimens{i}(1);
end
for k = 1:size(uloads,2)-1
uloads_temp{1,k} = A(k,max_idx(1,i))*DLC_dimens{i}{4};
uloads_temp{2,k} = A(k,min_idx(1,i))*DLC_dimens{i}{4};
uloads_temp{3,k} = A(k,max_idx(2,i))*DLC_dimens{i}{4};
uloads_temp{4,k} = A(k,min_idx(2,i))*DLC_dimens{i}{4};
uloads_temp{5,k} = A(k,max_idx(3,i))*DLC_dimens{i}{4};
uloads_temp{6,k} = A(k,min_idx(3,i))*DLC_dimens{i}{4};
uloads_temp{7,k} = A(k,max_idx(4,i))*DLC_dimens{i}{4};
uloads_temp{8,k} = A(k,min_idx(4,i))*DLC_dimens{i}{4};
uloads_temp{9,k} = A(k,max_idx(5,i))*DLC_dimens{i}{4};
uloads_temp{10,k} = A(k,min_idx(5,i))*DLC_dimens{i}{4};
uloads_temp{11,k} = A(k,max_idx(6,i))*DLC_dimens{i}{4};
uloads_temp{12,k} = A(k,min_idx(6,i))*DLC_dimens{i}{4};
uloads_temp{13,k} = A(k,max_idx(7,i))*DLC_dimens{i}{4};
uloads_temp{14,k} = A(k,min_idx(7,i))*DLC_dimens{i}{4};
uloads_temp{15,k} = A(k,max_idx(8,i))*DLC_dimens{i}{4};
uloads_temp{16,k} = A(k,min_idx(8,i))*DLC_dimens{i}{4};
end
if uloads_temp{1,1}(:) > uls{1,1}(:)
uls(1,:) = uloads_temp(1,:);
end
if uloads_temp{2,1}(:) < uls{2,1}(:)
uls(2,:) = uloads_temp(2,:);
end
if uloads_temp{3,2}(:) > uls{3,2}(:)
uls(3,:) = uloads_temp(3,:);
end
if uloads_temp{4,2}(:) < uls{4,2}(:)
uls(4,:) = uloads_temp(4,:);
end
if uloads_temp{5,3}(:) > uls{5,3}(:)
uls(5,:) = uloads_temp(5,:);
end
if uloads_temp{6,3}(:) < uls{6,3}(:)
uls(6,:) = uloads_temp(6,:);
end
if uloads_temp{7,4}(:) > uls{7,4}(:)
uls(7,:) = uloads_temp(7,:);
end
if uloads_temp{8,4}(:) < uls{8,4}(:)
uls(8,:) = uloads_temp(8,:);
end
if uloads_temp{9,5}(:) > uls{9,5}(:)
uls(9,:) = uloads_temp(9,:);
end
if uloads_temp{10,5}(:) < uls{10,5}(:)
uls(10,:) = uloads_temp(10,:);
end
if uloads_temp{11,6}(:) > uls{11,6}(:)
uls(11,:) = uloads_temp(11,:);
end
if uloads_temp{12,6}(:) < uls{12,6}(:)
uls(12,:) = uloads_temp(12,:);
end
if uloads_temp{13,7}(:) > uls{13,7}(:)
uls(13,:) = uloads_temp(3,:);
end
if uloads_temp{14,7}(:) < uls{14,7}(:)
uls(14,:) = uloads_temp(14,:);
end
if uloads_temp{15,8}(:) > uls{15,8}(:)
uls(15,:) = uloads_temp(15,:);
end
if uloads_temp{16,8}(:) < uls{16,8}(:)
uls(16,:) = uloads_temp(16,:);
end
fclose(fid);
end
Now comes the question: I was thinking of a procedure where
I generate a temporary uloads_temp matrix just with the first file;
Calculate the uloads matrix for the i-th file (i = 2:num_files)
Compare the terms on the diagonal between i-th uloads matrix and temporary uloads_temp:
a) if the elements of the i-th ulaods are major (minor) than the respective uloads_temp values
b) update uloads_temp rows the condition a) occurs.
I hope I explained everything properly. Could you please give me a hint on how to perform the described loops?
I thank you all in advance.
WKR,
Francesco
P.S. : everything could be reproduced by means fo matrices filled of random numbers; I just copied and pasted my code with reference to a file list.