我需要在MATLAB中导入许多表(.xlsx格式)。因此,我尝试使用for循环来执行此操作。
代码报告如下:
addpath('C:\...'); % here hide the actual path just for simplicity
numfiles = 50;
mydata = cell(1, numfiles);
for k = 1:numfiles
myfilename = sprintf('Table_%d.xlsx', k);
mydata{k} = importdata(myfilename);
end
即使这确实可行,也会由于持续时间(hh:mm:ss)和UTCdate数组转换为简单double或NaN而产生问题。
有人能提出解决此问题的方法吗?
答案 0 :(得分:0)
您可能可以使用新版Matlab中提供的readtable功能。这将创建一个matlab表对象,该对象应能满足您的需求。
选项(“ DatetimeType”,“文本”)应该可以帮助您解决时间问题(通过编写自定义代码来处理日期格式)。
答案 1 :(得分:0)
就您而言,我宁愿使用readtable
函数。它专门针对Excel文件,因此与importdata
或First Second Third
1 2 3
4 5 x
01:43:00 8 9
相比节省了一些时间。您可以通过以下方式使用它:
想象一下,您创建了一个如下所示的.xls文件:
xlsread
用[num,txt,raw] = xlsread('myExample.xlsx')
num =
1 2 3
4 5 NaN
NaN 8 9
txt =
'First' 'Second' 'Third'
'' '' ''
'' '' 'x'
'01:43:00' '' ''
raw =
'First' 'Second' 'Third'
[ 1] [ 2] [ 3]
[ 4] [ 5] 'x'
'01:43:00' [ 8] [ 9]
读取这些文件将总共输出三个矩阵。
xlsread
您可以通过多种不同的方式自定义导入,应用过滤器,忽略某些数据结构等。我建议您对class FollowUsersTableViewController: UIViewController{
@IBOutlet var tableView: UITableView!
private var viewIsHiddenObserver: NSKeyValueObservation?
let searchController = UISearchController(searchResultsController: nil)
var usersArray = [UserModel]()
var filteredUsers = [UserModel]()
var loggedInUser: User?
//
var databaseRef = Database.database().reference()
//usikker på den koden over
override func viewDidLoad() {
super.viewDidLoad()
//large title
self.title = "Discover"
if #available(iOS 11.0, *) {
self.navigationController?.navigationBar.prefersLargeTitles = true
} else {
// Fallback on earlier versions
}
self.tableView?.delegate = self
self.tableView?.dataSource = self
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
self.searchController.delegate = self;
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
self.loadProfileData()
}
func loadProfileData() {
databaseRef.child("profile").queryOrdered(byChild: "username").observe(.childAdded, with: { (snapshot) in
print(snapshot)
let userObj = Mapper<UserModel>().map(JSONObject: snapshot.value!)
userObj?.uid = snapshot.key
guard snapshot.key != self.loggedInUser?.uid else { return }
self.usersArray.append(userObj!)
})
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let dest = segue.destination as! UserProfileViewController
let obj = sender as! UserModel
let dict = ["uid": obj.uid!, "username": obj.username!, "photoURL": obj.photoURL, "bio": obj.bio]
dest.selectedUser = dict as [String : Any]
}
}
// MARK: - tableview methods
extension FollowUsersTableViewController: UITableViewDataSource,
UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return searchController.searchBar.text!.count >= 2 ?
filteredUsers.count : 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! FollowTableViewCell
let user = filteredUsers[indexPath.row]
cell.title?.text = user.username
if let url = URL(string: user.photoURL ?? "") {
cell.userImage?.sd_setImage(with: url, placeholderImage:
#imageLiteral(resourceName: "user_male"), options:
.progressiveDownload, completed: nil)
cell.userImage.sd_setIndicatorStyle(.gray)
cell.userImage.sd_showActivityIndicatorView()
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath:
IndexPath) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath:
IndexPath) {
self.performSegue(withIdentifier: "user", sender: self.filteredUsers[indexPath.row])
}
}
// MARK: - search methods
extension FollowUsersTableViewController:UISearchResultsUpdating,
UISearchControllerDelegate {
func updateSearchResults(for searchController: UISearchController) {
searchController.searchResultsController?.view.isHidden = false
filterContent(searchText: self.searchController.searchBar.text!)
self.tableView.reloadData()
}
func filterContent(searchText:String){
if searchText.count >= 2{
self.filteredUsers = self.usersArray.filter{ user in
return(user.username!.lowercased().contains(searchText.lowercased()))
}
}
}
}
进行Matlab documentation的操作。