Cant make my data analysis program to work using excel file

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

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 12 月 13 日

0 投票

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 件のコメント

Lucas Santana
Lucas Santana 2016 年 12 月 13 日
編集済み: Lucas Santana 2016 年 12 月 13 日
"selected_x2" manage to convert to right type necessary to use regexp. But i cant manage to copy Blink Dagger to a cell using the:
locs = find( ~cellfun(@isempty, regexp(selected_x2, '//=+$', 'lineanchors')) );
"locs" return a huge column of numbers.
"selected_x2" return a column with numbers and words but i cant find blink dagger anymore.
"selected_x" return a matrix and i can find blink inside cell [2,3] and dagger inside cell [2,4]
Thanks
Lucas Santana
Lucas Santana 2016 年 12 月 13 日
編集済み: Lucas Santana 2016 年 12 月 13 日
I did other program
function x=testt()
fid = fopen('items.txt','r');
text = textscan(fid,'%s','Delimiter','','endofline','');
text = text{1}{1};
fid = fclose(fid);
x = regexp(text,'(?<=//=================================================================================================================).*(?=//=================================================================================================================)','match');
end
The value of x is attached.
I just want blink dagger

サインインしてコメントする。

タグ

質問済み:

2016 年 12 月 13 日

編集済み:

2016 年 12 月 13 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by