Cant make my data analysis program to work using excel file
Ältere Kommentare anzeigen
Hi, im trying to get the text between the characters //=========... and //==========... from an excell file and create another excel file with an organized data base.
My program dont work after the line inBetweenText=regexp(x,'(?<=//===============...).*(?=//===============...)','match');
Im also trying to write into the excel file dynamically. The final result that im trying to achieve is something like the image file attached called "finalresult".
function x=dotadata()
Excel = actxserver('Excel.Application');
fullPathToExcelFile = 'C:\Users\Lucas\Documents\MATLAB\itensss.xlsx'; % Please modify this to the location of your excel file
ExcelWorkbook = Excel.workbooks.Open(fullPathToExcelFile,0,true);
WorkSheets = Excel.sheets;
TargetSheet = get(WorkSheets,'item','Sheet1');
Activate(TargetSheet);
DataRange = Excel.ActiveSheet.UsedRange;
r = DataRange.Address
ExcelWorkbook.Close
Excel.Quit
Excel.delete
clear Excel WorkSheets TargetSheet DataRange ExcelWorkbook
% Using the range returned to read data
r1 = regexp(r, ':', 'split');
end_cell = regexprep(r1{2}, '\$', '');
start_cell = 'A2';
range = [start_cell ':' end_cell];
[num,txt,raw] = xlsread('itensss.xlsx', 'Sheet1', range);
x=raw;
titulo = {'bonus_attack_speed','bonus_armor','bonus_damage'};
xlswrite('testitens.xlsx', titulo, 'Sheet1', 'A1');
inBetweenText=regexp(x,'(?<=//=================================================================================================================).*(?=//=================================================================================================================)','match');
for k=2:20
my_cell = sprintf( 'A%s',num2str(k) );
xlswrite('testitens.xlsx',inBetweenText,'Sheet1',my_cell);
end
end
The value of x is something like this:
NaN NaN NaN NaN NaN NaN NaN
NaN '//=================================================================================================================' NaN NaN NaN NaN NaN
NaN '//' 'Blink' 'dagger' NaN NaN NaN
NaN '//=================================================================================================================' NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN
NaN '//' 'Stats' NaN NaN NaN NaN
NaN '//-------------------------------------------------------------------------------------------------------------' NaN NaN NaN NaN NaN
NaN 'AbilityCastRange' 0 NaN NaN NaN NaN
NaN 'AbilityCastPoint' '0.0' NaN NaN NaN NaN
NaN 'AbilityCooldown' '12.0' NaN NaN NaN NaN
NaN 'AbilityManaCost' 0 NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN
Thanks
Antworten (1)
Walter Roberson
am 13 Dez. 2016
x=raw;
so x is a cell array containing everything from the spreadsheet, with a mix of text cells and numeric cells.
inBetweenText=regexp(x,'(?<=//=================================================================================================================).*(?=//=================================================================================================================)','match');
Here, regexp expects its first argument to be either a character vector or a cell array of character vectors. Your data does not satisfy that.
If you were to select from column 2 only the entries that were strings:
mask = cellfun(@ischar, x(:,2));
selected_x = x(mask, :);
selected_x2 = selected_x(:, 2);
and apply regexp() to selected_x2 then you would get further.
However, each cell entry is treated as being entirely independent of the others. That is a problem for you for regexp because you are trying to match between lines. If you were absolutely certain how many '=' were in the string you could use ismember() to do the matching instead of regexp, but if you want it to be variable then:
locs = find( ~cellfun(@isempty, regexp(selected_x2, '//=+$', 'lineanchors')) );
Now locs will be the indices of all the column 2 entries that are // followed by = to the end of the entry. You can process that as desired to extract entries from selected_x.
2 Kommentare
Lucas Santana
am 13 Dez. 2016
Bearbeitet: Lucas Santana
am 13 Dez. 2016
Lucas Santana
am 13 Dez. 2016
Bearbeitet: Lucas Santana
am 13 Dez. 2016
Kategorien
Mehr zu Spreadsheets finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!