I wanted to write a code to do this command for me but I have a problem with defining iterative part:
My requirement: I have 101 files which are ended with a number that can be used as a numerator like file_01 to file_101. I want to have "for loop" to do this: removing 3 columns from each file and adding 3 columns with different arrays to those files. The arrays should be read from a specific excel file.
could you please help me
clear all
clc
close all
files = dir('*.txt');
for i=1:length(files)
eval(['load ' files(i).name ' -ascii']);
end
para = xlsread('parameters.xlsx');
and for other part
T = size(x1);
j = T(:,1);
A = zeros(j,1);
for i =0:length(N)
% Deleting the first column
x1(:,1)=[];
newcol = zeros(j,1);
x1 = [newcol x1];
end
Try this:
data = xlsread('parameters.xlsx');
files = dir('*.txt');
for i = 1:length(files)
A = load(files(i).name); % load data from txt
A(1:3,:) = []; % remove first 3 columns
A = [A data(1:3,:)]; % add 3 column from excel data
dlmwrite(files(i).name,A); % re-write current file
end
Remember that number of rows from excel and txt files should be the same
Related
I have an excel document with two sheets. Sheet 1 has columns A-Q and Sheet 2 has columns A-H. What I need is a code that will copy the information in a row from sheet 1 to sheet 2 if the criteria is met. The criteria is the word "Awarded" in column L (Sheet 1).
Also is it possible to have only specific columns in the row copied?
A B C D E F G H I J K L M N
X X Awarded X X
I would like to have only columns C,D,M, and N copied from the row if the word "awarded" is in column L. This information would be copied to Sheet 2 in the following fashion
Sheet 1 Sheet 2
D --> B
C --> C
M --> D
N --> F
I hope I'm being clear. Thanks in advance and let me know if I need to clarify!+
This is the code I currently have, which works. Only problem is it copies the entire row of information into sheet 2 when I only want rows D,C,M, and N to be copied.
Sub testing()
Set a = Sheets("Sheet1")
Set b = Sheets("Sheet2")
Dim d
Dim j
d = 1
j = 2
Do Until IsEmpty(a.Range("L" & j))
If a.Range("L" & j) = "Awarded" Then
d = d + 1
b.Rows(d).Value = a.Rows(j).Value
End If
j = j + 1
Loop
End Sub
First what you should do is change your data structure. Assuming you are using Excel 2007 or later, there is a great feature called Tables. If you highlight all of your data and go to Insert->Table, select the "My Table Has Headers" checkbox, and press ok, you will see a nicely formatted table. Do that for both of the data sets on each sheet.
This is more than just pretty formatting though, it is what is called a ListObject. In your VBA code, use the following to reference it:
Dim Table1 as ListObject, Table 2 as ListObject
Dim HeaderIndex as Integer
Dim MyColumnRange as Range
Set Table1 = Sheet1.ListObjects("TableName1")
`Change the table name under Formulas->Name Manager
Set Table2 = Sheet1.ListObjects("TableName2")
HeaderIndex = Application.WorksheetFunction.Match("ColumnLHeaderName", _
Table1.HeaderRowRange, 0)
Set MyColumnRange = Table1.ListColumns(HeaderIndex).DataBodyRange
MyColumnRange.Select
At this point, the select statement is just to show you what range you are dealing with now. The HeaderIndex refers to the header sub component of the table ListObject. Using Match() will allow you to specify the name of the column header without hard coding it's position. (i.e. if your data starts in column A, the header value in column L will return HeaderIndex = 12)
Now that you know what column you want, you select the ListColumn object. Then, the DataBodyRange is used to select the range component of that object. This is the entire range in that column. You can then iterate down the list to find the data you want.
EDIT: Updated Example:
'Specify your ranges you will be copying from beforehand, adding as many as you need here.
HeaderIndex_D = Application.WorksheetFunction.Match("ColumnXHeaderName", _
Table1.HeaderRowRange, 0)
HeaderIndex_C = Application.WorksheetFunction.Match("ColumnXHeaderName", _
Table1.HeaderRowRange, 0)
HeaderIndex_M = Application.WorksheetFunction.Match("ColumnXHeaderName", _
Table1.HeaderRowRange, 0)
HeaderIndex_N = Application.WorksheetFunction.Match("ColumnXHeaderName", _
Table1.HeaderRowRange, 0)
Set ColumnRange_D= Table1.ListColumns(HeaderIndex_D).DataBodyRange
Set ColumnRange_C= Table1.ListColumns(HeaderIndex_C).DataBodyRange
Set ColumnRange_M= Table1.ListColumns(HeaderIndex_M).DataBodyRange
Set ColumnRange_N= Table1.ListColumns(HeaderIndex_N).DataBodyRange
'Now, loop through each row that exists in your table. If the testing
'condition contained in MyColumnRange you previously defined is met,
'then assign the destination cell (which can be defined in the same way
'as above) equal to the lookup range's current row value (specified by i)
For i = 1 to MyColumnRange.Rows.Count
If MyColumnRange(i) = "Awarded" Then
DestinationCell1.Value = ColumnRange_D(i)
DestinationCell2.Value = ColumnRange_C(i)
DestinationCell3.Value = ColumnRange_M(i)
DestinationCell4.Value = ColumnRange_N(i)
End If
Next i
I have a folder that contains pictures of receipts that are named in a specific way. Date first in reverse format (ex. 21/11/2015 -> 15_11_21) followed by a space and then the value of the receipt (ex. 18,45 -> 18_45)
Let's say the files are stored in location C:\pictures\receipts. In this folder I have 3 files:
15_11_21 18_45.jpg
15_11_22 115_28.jpg
15_12_02 3_00.jpg
I want to create an array that has 3 columns. The first column contains the date of the receipt in normal format, the second column contains the value in negative and the third column has the absolute path of the file. The array should be like this:
Receipts = [21/11/2015|-18,45 |C:\pictures\receipts\15_11_21 18_45.jpg
22/11/2015|-115,28|C:\pictures\receipts\15_11_22 115_28.jpg
02/12/2015| -3,00 |C:\pictures\receipts\15_12_02 3_00.jpg];
I tried modifying/combining various functions like getting the full path:
[status, list] = system( 'dir /B /S *.mp3' );
result = textscan( list, '%s', 'delimiter', '\n' );
fileList = result{1}
strsplit to separate the values of the filenames, and even this function, but I cannot get the desired result.
It looks like strsplit should do what you want. Try:
strsplit (filename, {' ', '.'})
Also, I would use dir rather than system, since it is probably more independent of changes in the operating system.
A little bit "hacky":
filename = 'C:\pictures\receipts\15_11_21 18_45.jpg';
filename = strsplit(filename,'\');
filename = filename(end);
d = textscan('15_11_21 18_45.jpg', '%d_%d_%d %d_%d.jpg');
day = d{1};
month = d{2};
year = d{3};
a = -d{4};
b = d{5};
receipt = sprintf('%d/%d/20%d|%d,%d|%s', year, month, day, a, b, filename{1})
Have a look at formatting operators (e.g. type doc sprintf). You may want to add some flags for justification/spacings.
One option, utilizing regular expressions and a data structure as the final output:
% Get list of JPEGs in the current directory + subdirectories
[~, list] = system( 'dir /B /S *.jpg' );
result = textscan( list, '%s', 'delimiter', '\n' );
fileList = result{1};
% Split out file names, could use a regex but why bother. Using cellfun
% rather than an explicit loop
[~, filenames] = cellfun(#fileparts, fileList, 'UniformOutput', false);
% Used named tokens to pull out our data for analysis
Receipts = regexp(filenames, '(?<date>\d*_\d*_\d*)\s*(?<cost>\d*_\d*)', 'names');
Receipts = [Receipts{:}]; % Dump out our nested data
[Receipts(:).fullpath] = fileList{:}; % Add file path to our structure
% Reformat costs
% Replace underscore with decimal, convert to numeric array and negate
tmp = -str2double(strrep({Receipts(:).cost}, '_', '.'));
tmp = num2cell(tmp); % Necessary intermediate step, because MATLAB...
[Receipts(:).cost] = tmp{:}; % Replace field in our data structure
clear tmp
% Reformat dates
formatIn = 'yy_mm_dd';
formatOut = 'dd/mm/yyyy';
pivotYear = 2000; % Pivot year needed since we have 2-digit years
% datenum needed because we have a custom input date format
tmp = datestr(datenum({Receipts(:).date}, formatIn, pivotYear), formatOut);
tmp = cellstr(tmp); % Necessary intermediate step, because MATLAB...
[Receipts(:).date] = tmp{:};
clear tmp
This results in a structure array, Receipts. I went this route because it's more explicit to access the data in the future. For example, if I wanted the cost of my 2nd receipt, I could do:
Employee2Cost = Receipts(2).cost;
Which returns:
Employee2Cost =
-115.2800
i have a cell array with dimensions 1x16384 consists of 16384 numbers.
I tried using cell2mat(), but i get one element with all those numbers in it.
How do i convert it to an array with 1x16384 dimension.
Assuming B= cell2mat(A);
A looks like;
Columns 16382 through 16384
'15.849' '16.337' '14.872'
where as B looks like;
B =
-8.921-8.433-2.5745.23911.58714.02811.5876.7041.821-1.109-2.085-1.5970.3562.
console ;
class(A), class(A{1}), size(A), size(A{1})
ans =
cell
ans =
char
ans =
1 16384
ans =
1 6
For mcve,
Csv input file
DELIMITER = ' ';
HEADERLINES = 3;
% Import the file
newData1 = importdata('C:\Python34\co2a0000364.csv', DELIMITER, HEADERLINES);
% Create new variables in the base workspace from those fields.
vars = fieldnames(newData1);
for i = 1:length(vars)
assignin('base', vars{i}, newData1.(vars{i}));
end
new = textdata(6:16452,4);
A = new;
for z = 1:63
A(i*257)=[];
end
B = A'
A= B;
A= cell2mat(B)
B= A
You way of importing data is quite clumsy. I invite you to read the textscan function documentation, which allows much more flexibility in importing mixed data type (text and numeric data).
I propose you another way of importing, which uses textscan:
%% // Import only the 4th column of data
fid = fopen('co2a0000364.csv') ;
M = textscan( fid , '%*s%*s%*s%f' , 'Delimiter',' ' , 'HeaderLines',4) ;
fclose(fid) ;
M = cell2mat(M) ;
%% // reshape to have each channel in its own colum
M = reshape( M , 257 , [] ) ;
%% // Delete the channel number from the data table
M(1,:) = [] ;
This will give you a nice 256*64 matrix, containing the data for each of the 64 channels in one column.
if you really want them all in sequence in one column, just add:
M = M(:) ;
to the end of the code.
Explanation note: the textscan format specifier I use: '%*s%*s%*s%f' tells the function to read 4 elements per line:
- 3x strings : %s
- 1x floating point number: %f
adding the character * in the format specifier (for example %*s) for an element tells the function to ignore the element, so it does not put it into the output.
I have an array with a set of chronological serial numbers and another source array with random serial numbers associated with a numeric value. The code creates a new cell array in MATLAB with the perfectly chronological serial numbers in one column and in the next column it inserts the associated numeric value if the serial numbers match in both original source arrays. If they don't the code simply copies the previous associated value until there is a new match.
j = 1;
A = {random{1:end,1}};
B = cell2mat(A);
value = random{1,2};
data = cell(length(serial), 1);
data(:,1) = serial(:,1);
h = waitbar(0,'Please Wait...');
steps = length(serial);
for k = 1:length(serial)
[row1, col1, vec1] = find(B == serial{k,1});
tf1 = isempty(vec1);
if (tf1 == 0)
prices = random{col1,2};
data(j,2) = num2cell(value);
j = j + 1;
else
data(j,2) = num2cell(value);
j = j + 1;
end
waitbar(k/steps,h,['Please Wait... ' num2str(k/steps*100) ' %'])
end
close(h);
Right now, the run-time for the code is approximately 4 hours. I would like to make this code run faster. Please suggest any methods to do so.
UPDATE
source input (serial)
1
2
3
4
5
6
7
source input (random)
1 100
2 105
4 106
7 107
desired output (data)
SR No Value
1 100
2 105
3 105
4 106
5 106
6 106
7 107
Firstly, run the MATLAB profiler (see 'doc profile') and see where the bulk of the execution time is occuring.
Secondly, don't update the waitbar on every iteration> Particularly if serial contains a large (> 100) number of elements.
Do something like:
if (mod(k, 100)==0) % update on every 100th iteration
waitbar(k/steps,h,['Please Wait... ' num2str(k/steps*100) ' %'])
end
Some points:
Firstly it would help a lot if you gave us some sample input and output data.
Why do you initialize data as one column and then fill it's second in the loop? Rather initialize it as 2 columns upfront: data = cell(length(serial), 2);
Is j ever different from k, they look identical to me and you could just drop both the j = j + 1 lines.
tf1 = isempty(vec1); if (tf1 == 0)... is the same as the single line: if (!isempty(vec1)) or even better if(isempty(vec1)) and then swap the code from your else and your if.
But I think you can probably find a fast vecotrized solution if you provide some (short) sample input and output data.
I have a CSV file, I want to read this file and do some pre-calculations on each row to see for example that row is useful for me or not and if yes I save it to a new CSV file.
can someone give me an example?
in more details this is how my data looks like: (string,float,float) the numbers are coordinates.
ABC,51.9358183333333,4.183255
ABC,51.9353866666667,4.1841
ABC,51.9351716666667,4.184565
ABC,51.9343083333333,4.186425
ABC,51.9343083333333,4.186425
ABC,51.9340916666667,4.18688333333333
basically i want to save the rows that have for distances more than 50 or 50 in a new file.the string field should also be copied.
thanks
You could actually use xlsread to accomplish this. After first placing your sample data above in a file 'input_file.csv', here is an example for how you can get the numeric values, text values, and the raw data in the file from the three outputs from xlsread:
>> [numData,textData,rawData] = xlsread('input_file.csv')
numData = % An array of the numeric values from the file
51.9358 4.1833
51.9354 4.1841
51.9352 4.1846
51.9343 4.1864
51.9343 4.1864
51.9341 4.1869
textData = % A cell array of strings for the text values from the file
'ABC'
'ABC'
'ABC'
'ABC'
'ABC'
'ABC'
rawData = % All the data from the file (numeric and text) in a cell array
'ABC' [51.9358] [4.1833]
'ABC' [51.9354] [4.1841]
'ABC' [51.9352] [4.1846]
'ABC' [51.9343] [4.1864]
'ABC' [51.9343] [4.1864]
'ABC' [51.9341] [4.1869]
You can then perform whatever processing you need to on the numeric data, then resave a subset of the rows of data to a new file using xlswrite. Here's an example:
index = sqrt(sum(numData.^2,2)) >= 50; % Find the rows where the point is
% at a distance of 50 or greater
% from the origin
xlswrite('output_file.csv',rawData(index,:)); % Write those rows to a new file
If you really want to process your file line by line, a solution might be to use fgetl:
Open the data file with fopen
Read the next line into a character array using fgetl
Retreive the data you need using sscanf on the character array you just read
Perform any relevant test
Output what you want to another file
Back to point 2 if you haven't reached the end of your file.
Unlike the previous answer, this is not very much in the style of Matlab but it might be more efficient on very large files.
Hope this will help.
You cannot read text strings with csvread.
Here is another solution:
fid1 = fopen('test.csv','r'); %# open csv file for reading
fid2 = fopen('new.csv','w'); %# open new csv file
while ~feof(fid1)
line = fgets(fid1); %# read line by line
A = sscanf(line,'%*[^,],%f,%f'); %# sscanf can read only numeric data :(
if A(2)<4.185 %# test the values
fprintf(fid2,'%s',line); %# write the line to the new file
end
end
fclose(fid1);
fclose(fid2);
Just read it in to MATLAB in one block
fid = fopen('file.csv');
data=textscan(fid,'%s %f %f','delimiter',',');
fclose(fid);
You can then process it using logical addressing
ind50 = data{2}>=50 ;
ind50 is then an index of the rows where column 2 is greater than 50. So
data{1}(ind50)
will list all the strings for the rows of interest.
Then just use fprintf to write out your data to the new file
here is the doc to read a csv : http://www.mathworks.com/access/helpdesk/help/techdoc/ref/csvread.html
and to write : http://www.mathworks.com/access/helpdesk/help/techdoc/ref/csvwrite.html
EDIT
An example that works :
file.csv :
1,50,4.1
2,49,4.2
3,30,4.1
4,71,4.9
5,51,4.5
6,61,4.1
the code :
File = csvread('file.csv')
[m,n] = size(File)
index=1
temp=0
for i = 1:m
if (File(i,2)>=50)
temp = temp + 1
end
end
Matrix = zeros(temp, 3)
for j = 1:m
if (File(j,2)>=50)
Matrix(index,1) = File(j,1)
Matrix(index,2) = File(j,2)
Matrix(index,3) = File(j,3)
index = index + 1
end
end
csvwrite('outputFile.csv',Matrix)
and the output file result :
1,50,4.1
4,71,4.9
5,51,4.5
6,61,4.1
This isn't probably the best solution but it works! We can read the CSV file, control the distance of each row and save it in a new file.
Hope it will help!